-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmsSqlConnection.go
More file actions
120 lines (110 loc) · 3.31 KB
/
msSqlConnection.go
File metadata and controls
120 lines (110 loc) · 3.31 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
package main
import (
"context"
"database/sql"
"log"
"net/url"
"time"
)
type MsSqlDatabase struct {
server string
database string
username string
password string
ctx context.Context
connection *sql.DB
sqlite *SqlLiteDatabase
}
func (m *MsSqlDatabase) Initialize() error {
query := url.Values{}
query.Add("database", m.database)
url := &url.URL{
Scheme: "sqlserver",
User: url.UserPassword(m.username, m.password),
Host: m.server,
RawQuery: query.Encode(),
}
db, err := sql.Open("sqlserver", url.String())
if err != nil {
m.sqlite.WriteLog(ERROR, err, "msSqlConnection.go", "Initialize")
return err
}
m.connection = db
return db.PingContext(m.ctx)
}
func (m *MsSqlDatabase) Disconnect() error {
if err := m.connection.Close(); err != nil {
m.sqlite.WriteLog(ERROR, err, "msSqlConnection.go", "Disconnect")
return err
}
return nil
}
func (m *MsSqlDatabase) FindUsers(target string) (QueryResult[UserPermissionResult], error) {
tsql := `
SELECT distinct p.name
FROM sys.database_principals p
JOIN sys.database_permissions dp on dp.grantee_principal_id = p.principal_id
LEFT JOIN sys.objects o on o.object_id = dp.major_id
`
output := QueryResult[UserPermissionResult]{
Duration: time.Since(time.Now()),
Data: nil,
}
outputData := make([]UserPermissionResult, 0)
rows, err := m.connection.QueryContext(m.ctx, tsql)
if err != nil {
m.sqlite.WriteLog(ERROR, err, "msSqlConnection.go", "QueryUserPermissions")
output.Data = nil
return output, err
}
for rows.Next() {
temp := UserPermissionResult{}
err = rows.Scan(&temp.Name, &temp.PermissionName, &temp.ObjectName)
if err != nil {
m.sqlite.WriteLog(ERROR, err, "msSqlConnection.go", "QueryUserPermissions")
log.Println("Error reading row from User Permissions result.")
}
outputData = append(outputData, temp)
}
output.Data = outputData
return output, nil
}
func (m *MsSqlDatabase) FindUserPermissions(user string, target string) (QueryResult[UserPermissionResult], error) {
tsql := `
SELECT p.name, dp.permission_name, o.name
FROM sys.database_principals p
JOIN sys.database_permissions dp on dp.grantee_principal_id = p.principal_id
LEFT JOIN sys.objects o on o.object_id = dp.major_id
WHERE p.name = @user and (@target = '' or o.name = @target)
`
output := QueryResult[UserPermissionResult]{
Duration: time.Since(time.Now()),
Data: nil,
}
outputData := make([]UserPermissionResult, 0)
rows, err := m.connection.QueryContext(m.ctx, tsql, sql.Named("user", user), sql.Named("target", target))
if err != nil {
m.sqlite.WriteLog(ERROR, err, "msSqlConnection.go", "QueryUserPermissions")
output.Data = nil
return output, err
}
for rows.Next() {
temp := UserPermissionResult{}
err = rows.Scan(&temp.Name, &temp.PermissionName, &temp.ObjectName)
if err != nil {
m.sqlite.WriteLog(ERROR, err, "msSqlConnection.go", "QueryUserPermissions")
log.Println("Error reading row from User Permissions result.")
log.Println(err)
}
log.Println(temp)
outputData = append(outputData, temp)
}
output.Data = outputData
return output, nil
}
func (m *MsSqlDatabase) GrantPermissions(user string, target string, permission string) (bool, error) {
return false, nil
}
func (m *MsSqlDatabase) RemovePermission(user string, target string, permission string) (bool, error) {
return false, nil
}