blob: 50674af7625111b15d4b2abf353c6697e0b17e6d [file] [log] [blame]
Adrià Vilanova Martínez25e12112021-08-25 13:48:06 +02001package db
2
3import (
4 "context"
5 "database/sql"
6 "fmt"
7
8 pb "gomodules.avm99963.com/twpt-server/api_proto"
9)
10
11func GetAuthorizedUserById(db *sql.DB, ctx context.Context, id int32) (*pb.KillSwitchAuthorizedUser, error) {
12 query := db.QueryRowContext(ctx, "SELECT user_id, google_uid, email, access_level FROM KillSwitchAuthorizedUser WHERE user_id = ?", id)
13 var u pb.KillSwitchAuthorizedUser
14 if err := query.Scan(&u.Id, &u.GoogleUid, &u.Email, &u.AccessLevel); err != nil {
15 if err == sql.ErrNoRows {
16 return nil, nil
17 }
18 return nil, fmt.Errorf("GetAuthorizedUserById: %v.", err)
19 }
20 return &u, nil
21}
22
Adrià Vilanova Martínezc147b6a2021-09-01 17:25:38 +020023func AddAuthorizedUser(db *sql.DB, ctx context.Context, u *pb.KillSwitchAuthorizedUser, currentUser *pb.KillSwitchAuthorizedUser) error {
Adrià Vilanova Martínez25e12112021-08-25 13:48:06 +020024 tx, err := db.BeginTx(ctx, nil)
25 if err != nil {
26 return err
27 }
28
29 result, err := db.ExecContext(ctx, "INSERT INTO KillSwitchAuthorizedUser (google_uid, email, access_level) VALUES (?, ?, ?)", u.GoogleUid, u.Email, u.AccessLevel)
30 if err != nil {
31 tx.Rollback()
32 return err
33 }
34
35 id, err := result.LastInsertId()
36 if err != nil {
37 tx.Rollback()
38 return err
39 }
40
41 u.Id = int32(id)
42
43 logEntry := &pb.KillSwitchAuditLogEntry{
Adrià Vilanova Martínezc147b6a2021-09-01 17:25:38 +020044 User: currentUser,
Adrià Vilanova Martínez25e12112021-08-25 13:48:06 +020045 Description: &pb.KillSwitchAuditLogEntry_AuthorizedUserAdded_{
46 &pb.KillSwitchAuditLogEntry_AuthorizedUserAdded{
47 User: u,
48 },
49 },
50 }
51 if err := AddKillSwitchAuditLogEntry(tx, ctx, logEntry); err != nil {
52 tx.Rollback()
53 return err
54 }
55
56 return tx.Commit()
57}
58
Adrià Vilanova Martínezc147b6a2021-09-01 17:25:38 +020059func UpdateAuthorizedUser(db *sql.DB, ctx context.Context, id int32, newUser *pb.KillSwitchAuthorizedUser, currentUser *pb.KillSwitchAuthorizedUser) error {
Adrià Vilanova Martínez25e12112021-08-25 13:48:06 +020060 oldUser, err := GetAuthorizedUserById(db, ctx, id)
61 if err != nil {
62 return err
63 }
64 if oldUser == nil {
65 return fmt.Errorf("Such user doesn't exist")
66 }
67
68 tx, err := db.BeginTx(ctx, nil)
69 if err != nil {
70 return err
71 }
72
73 if _, err := tx.ExecContext(ctx, "UPDATE KillSwitchAuthorizedUser SET google_uid = ?, email = ?, access_level = ? WHERE user_id = ?", newUser.GoogleUid, newUser.Email, newUser.AccessLevel, id); err != nil {
74 tx.Rollback()
75 return err
76 }
77
78 newUser.Id = id
79
80 logEntry := &pb.KillSwitchAuditLogEntry{
Adrià Vilanova Martínezc147b6a2021-09-01 17:25:38 +020081 User: currentUser,
Adrià Vilanova Martínez25e12112021-08-25 13:48:06 +020082 Description: &pb.KillSwitchAuditLogEntry_AuthorizedUserUpdated_{
83 &pb.KillSwitchAuditLogEntry_AuthorizedUserUpdated{
84 Transformation: &pb.AuthorizedUserTransformation{
85 Old: oldUser,
86 New: newUser,
87 },
88 },
89 },
90 }
91 if err := AddKillSwitchAuditLogEntry(tx, ctx, logEntry); err != nil {
92 tx.Rollback()
93 return err
94 }
95
96 return tx.Commit()
97}
98
Adrià Vilanova Martínezc147b6a2021-09-01 17:25:38 +020099func DeleteAuthorizedUser(db *sql.DB, ctx context.Context, id int32, currentUser *pb.KillSwitchAuthorizedUser) error {
Adrià Vilanova Martínez25e12112021-08-25 13:48:06 +0200100 u, err := GetAuthorizedUserById(db, ctx, id)
101 if err != nil {
102 return err
103 }
104 if u == nil {
105 return fmt.Errorf("Such user doesn't exist")
106 }
107
108 tx, err := db.BeginTx(ctx, nil)
109 if err != nil {
110 return err
111 }
112
113 if _, err := tx.ExecContext(ctx, "DELETE FROM KillSwitchAuthorizedUser WHERE user_id = ?", id); err != nil {
114 tx.Rollback()
115 return err
116 }
117
118 logEntry := &pb.KillSwitchAuditLogEntry{
Adrià Vilanova Martínezc147b6a2021-09-01 17:25:38 +0200119 User: currentUser,
Adrià Vilanova Martínez25e12112021-08-25 13:48:06 +0200120 Description: &pb.KillSwitchAuditLogEntry_AuthorizedUserDeleted_{
121 &pb.KillSwitchAuditLogEntry_AuthorizedUserDeleted{
122 OldUser: u,
123 },
124 },
125 }
126 if err := AddKillSwitchAuditLogEntry(tx, ctx, logEntry); err != nil {
127 tx.Rollback()
128 return err
129 }
130
131 return tx.Commit()
132}
133
134func ListAuthorizedUsers(db *sql.DB, ctx context.Context) ([]*pb.KillSwitchAuthorizedUser, error) {
135 var rows *sql.Rows
136 var err error
137 rows, err = db.QueryContext(ctx, "SELECT user_id, google_uid, email, access_level FROM KillSwitchAuthorizedUser")
138 if err != nil {
139 return nil, fmt.Errorf("ListAuthorizedUsers: %v", err)
140 }
141 defer rows.Close()
142
143 var users []*pb.KillSwitchAuthorizedUser
144 for rows.Next() {
145 var u pb.KillSwitchAuthorizedUser
146 if err := rows.Scan(&u.Id, &u.GoogleUid, &u.Email, &u.AccessLevel); err != nil {
147 return nil, fmt.Errorf("ListAuthorizedUsers: %v", err)
148 }
149 users = append(users, &u)
150 }
151 if err := rows.Err(); err != nil {
152 return nil, fmt.Errorf("ListAuthorizedUsers: %v", err)
153 }
154 return users, nil
155}