Adrià Vilanova Martínez | 25e1211 | 2021-08-25 13:48:06 +0200 | [diff] [blame] | 1 | package db |
| 2 | |
| 3 | import ( |
| 4 | "context" |
| 5 | "database/sql" |
| 6 | "fmt" |
| 7 | |
| 8 | pb "gomodules.avm99963.com/twpt-server/api_proto" |
| 9 | ) |
| 10 | |
| 11 | func 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ínez | c147b6a | 2021-09-01 17:25:38 +0200 | [diff] [blame] | 23 | func AddAuthorizedUser(db *sql.DB, ctx context.Context, u *pb.KillSwitchAuthorizedUser, currentUser *pb.KillSwitchAuthorizedUser) error { |
Adrià Vilanova Martínez | 25e1211 | 2021-08-25 13:48:06 +0200 | [diff] [blame] | 24 | 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ínez | c147b6a | 2021-09-01 17:25:38 +0200 | [diff] [blame] | 44 | User: currentUser, |
Adrià Vilanova Martínez | 25e1211 | 2021-08-25 13:48:06 +0200 | [diff] [blame] | 45 | 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ínez | c147b6a | 2021-09-01 17:25:38 +0200 | [diff] [blame] | 59 | func UpdateAuthorizedUser(db *sql.DB, ctx context.Context, id int32, newUser *pb.KillSwitchAuthorizedUser, currentUser *pb.KillSwitchAuthorizedUser) error { |
Adrià Vilanova Martínez | 25e1211 | 2021-08-25 13:48:06 +0200 | [diff] [blame] | 60 | 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ínez | c147b6a | 2021-09-01 17:25:38 +0200 | [diff] [blame] | 81 | User: currentUser, |
Adrià Vilanova Martínez | 25e1211 | 2021-08-25 13:48:06 +0200 | [diff] [blame] | 82 | 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ínez | c147b6a | 2021-09-01 17:25:38 +0200 | [diff] [blame] | 99 | func DeleteAuthorizedUser(db *sql.DB, ctx context.Context, id int32, currentUser *pb.KillSwitchAuthorizedUser) error { |
Adrià Vilanova Martínez | 25e1211 | 2021-08-25 13:48:06 +0200 | [diff] [blame] | 100 | 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ínez | c147b6a | 2021-09-01 17:25:38 +0200 | [diff] [blame] | 119 | User: currentUser, |
Adrià Vilanova Martínez | 25e1211 | 2021-08-25 13:48:06 +0200 | [diff] [blame] | 120 | 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 | |
| 134 | func 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 | } |