@@ -94,21 +94,47 @@ func (s *PostgreSQLRepository) CreateDatabaseIfNotExists(ctx context.Context, da
94
94
}
95
95
}
96
96
97
- func (s * PostgreSQLRepository ) SetupUser (ctx context.Context , database string , user string , password string ) error {
97
+ type PostgresqlUser struct {
98
+ Database string
99
+ Username string
100
+ Password string
101
+ Roles []string
102
+ Grants []Grant
103
+ }
104
+
105
+ type Grant struct {
106
+ Object string
107
+ ObjectName string
108
+ User string
109
+ Privileges []Privilege
110
+ }
111
+
112
+ type Privilege string
113
+
114
+ var SelectPrivilege Privilege = "SELECT"
115
+ var AlPrivilege Privilege = "ALL"
116
+
117
+ func (s * PostgreSQLRepository ) SetupUser (ctx context.Context , user PostgresqlUser ) error {
98
118
if err := s .createUserIfNotExists (ctx , user ); err != nil {
99
- return err
119
+ return fmt . Errorf ( "failed to create user: %w" , err )
100
120
}
101
- if err := s .setPasswordForUser (ctx , user , password ); err != nil {
102
- return err
121
+ if err := s .setPasswordForUser (ctx , user ); err != nil {
122
+ return fmt . Errorf ( "failed to set password: %w" , err )
103
123
}
104
- if err := s .grantAllPrivileges (ctx , database , user ); err != nil {
105
- return err
124
+ if err := s .grantAllPrivileges (ctx , user ); err != nil {
125
+ return fmt .Errorf ("failed to grant all privileges: %w" , err )
126
+ }
127
+ if err := s .grantRoles (ctx , user ); err != nil {
128
+ return fmt .Errorf ("failed to grant roles: %w" , err )
129
+ }
130
+ if err := s .grantRules (ctx , user ); err != nil {
131
+ return fmt .Errorf ("failed to apply grant rules: %w" , err )
106
132
}
107
133
return nil
108
134
}
109
135
110
- func (s * PostgreSQLRepository ) DropUser (ctx context.Context , database string , user string ) error {
111
- if err := s .RevokeAllPrivileges (ctx , database , user ); err != nil {
136
+ func (s * PostgreSQLRepository ) DropUser (ctx context.Context , user PostgresqlUser ) error {
137
+ if err := s .RevokeAllPrivileges (ctx , user ); err != nil {
112
138
return err
113
139
}
114
140
if err := s .dropUserIfNotExist (ctx , user ); err != nil {
@@ -126,14 +152,14 @@ func (s *PostgreSQLRepository) EnableExtension(ctx context.Context, db, name str
126
152
return nil
127
153
}
128
154
129
- func (s * PostgreSQLRepository ) createUserIfNotExists (ctx context.Context , user string ) error {
155
+ func (s * PostgreSQLRepository ) createUserIfNotExists (ctx context.Context , user PostgresqlUser ) error {
130
156
if userExists , err := s .doesUserExist (ctx , user ); err != nil {
131
157
return err
132
158
} else {
133
159
if userExists {
134
160
return nil
135
161
}
136
- if _ , err := s .conn .Exec (ctx , fmt .Sprintf ("CREATE USER %s;" , (pgx.Identifier {user }).Sanitize ())); err != nil {
162
+ if _ , err := s .conn .Exec (ctx , fmt .Sprintf ("CREATE USER %s;" , (pgx.Identifier {user . Username }).Sanitize ())); err != nil {
137
163
return err
138
164
} else {
139
165
if userExistsNow , err := s .doesUserExist (ctx , user ); err != nil {
@@ -154,14 +180,14 @@ func (s *PostgreSQLRepository) createExtension(ctx context.Context, db, name str
154
180
return err
155
181
}
156
182
157
- func (s * PostgreSQLRepository ) dropUserIfNotExist (ctx context.Context , user string ) error {
183
+ func (s * PostgreSQLRepository ) dropUserIfNotExist (ctx context.Context , user PostgresqlUser ) error {
158
184
if userExists , err := s .doesUserExist (ctx , user ); err != nil {
159
185
return err
160
186
} else {
161
187
if ! userExists {
162
188
return nil
163
189
}
164
- if _ , err := s .conn .Exec (ctx , fmt .Sprintf ("DROP USER %s;" , (pgx.Identifier {user }).Sanitize ())); err != nil {
190
+ if _ , err := s .conn .Exec (ctx , fmt .Sprintf ("DROP USER %s;" , (pgx.Identifier {user . Username }).Sanitize ())); err != nil {
165
191
return err
166
192
} else {
167
193
if userExistsNow , err := s .doesUserExist (ctx , user ); err != nil {
@@ -177,23 +203,48 @@ func (s *PostgreSQLRepository) dropUserIfNotExist(ctx context.Context, user stri
177
203
}
178
204
}
179
205
180
- func (s * PostgreSQLRepository ) setPasswordForUser (ctx context.Context , user string , password string ) error {
181
- password , err := s .conn .PgConn ().EscapeString (password )
206
+ func (s * PostgreSQLRepository ) setPasswordForUser (ctx context.Context , user PostgresqlUser ) error {
207
+ password , err := s .conn .PgConn ().EscapeString (user . Password )
182
208
if err != nil {
183
209
return err
184
210
}
185
211
186
- _ , err = s .conn .Exec (ctx , fmt .Sprintf ("ALTER USER %s WITH ENCRYPTED PASSWORD '%s';" , (pgx.Identifier {user }).Sanitize (), password ))
212
+ _ , err = s .conn .Exec (ctx , fmt .Sprintf ("ALTER USER %s WITH ENCRYPTED PASSWORD '%s';" , (pgx.Identifier {user . Username }).Sanitize (), password ))
187
213
return err
188
214
}
189
215
190
- func (s * PostgreSQLRepository ) grantAllPrivileges (ctx context.Context , database string , user string ) error {
191
- _ , err := s .conn .Exec (ctx , fmt .Sprintf ("GRANT ALL PRIVILEGES ON DATABASE %s TO %s;" , (pgx.Identifier {database }).Sanitize (), (pgx.Identifier {user }).Sanitize ()))
216
+ func (s * PostgreSQLRepository ) grantAllPrivileges (ctx context.Context , user PostgresqlUser ) error {
217
+ _ , err := s .conn .Exec (ctx , fmt .Sprintf ("GRANT ALL PRIVILEGES ON DATABASE %s TO %s;" , (pgx.Identifier {user . Database }).Sanitize (), (pgx.Identifier {user . Username }).Sanitize ()))
192
218
return err
193
219
}
194
220
195
- func (s * PostgreSQLRepository ) RevokeAllPrivileges (ctx context.Context , database string , user string ) error {
196
- _ , err := s .conn .Exec (ctx , fmt .Sprintf ("REVOKE ALL PRIVILEGES ON DATABASE %s FROM %s;" , (pgx.Identifier {database }).Sanitize (), (pgx.Identifier {user }).Sanitize ()))
221
+ func (s * PostgreSQLRepository ) grantRoles (ctx context.Context , user PostgresqlUser ) error {
222
+ for _ , role := range user .Roles {
223
+ _ , err := s .conn .Exec (ctx , fmt .Sprintf ("GRANT %s TO %s;" , (pgx.Identifier {role }).Sanitize (), (pgx.Identifier {user .Username }).Sanitize ()))
224
+ if err != nil {
225
+ return err
226
+ }
227
+ }
228
+
229
+ return nil
230
+ }
231
+
232
+ func (s * PostgreSQLRepository ) grantRules (ctx context.Context , user PostgresqlUser ) error {
233
+ for _ , grant := range user .Grants {
234
+ for _ , p := range grant .Privileges {
235
+ _ , err := s .conn .Exec (ctx , fmt .Sprintf ("GRANT %s ON %s %s TO %s;" , string (p ), grant .Object , (pgx.Identifier {grant .ObjectName }).Sanitize (), (pgx.Identifier {user .Username }).Sanitize ()))
236
+ if err != nil {
237
+ return err
238
+ }
239
+ }
240
+
241
+ }
242
+
243
+ return nil
244
+ }
245
+
246
+ func (s * PostgreSQLRepository ) RevokeAllPrivileges (ctx context.Context , user PostgresqlUser ) error {
247
+ _ , err := s .conn .Exec (ctx , fmt .Sprintf ("REVOKE ALL PRIVILEGES ON DATABASE %s FROM %s;" , (pgx.Identifier {user .Database }).Sanitize (), (pgx.Identifier {user .Username }).Sanitize ()))
197
248
return err
198
249
}
199
250
@@ -214,14 +265,14 @@ func (s *PostgreSQLRepository) doesDatabaseExist(ctx context.Context, database s
214
265
return result == 1 , nil
215
266
}
216
267
217
- func (s * PostgreSQLRepository ) doesUserExist (ctx context.Context , user string ) (bool , error ) {
218
- user , err := s .conn .PgConn ().EscapeString (user )
268
+ func (s * PostgreSQLRepository ) doesUserExist (ctx context.Context , user PostgresqlUser ) (bool , error ) {
269
+ username , err := s .conn .PgConn ().EscapeString (user . Username )
219
270
if err != nil {
220
271
return false , err
221
272
}
222
273
223
274
var result int64
224
- err = s .conn .QueryRow (ctx , fmt .Sprintf ("SELECT 1 FROM pg_roles WHERE rolname='%s';" , user )).Scan (& result )
275
+ err = s .conn .QueryRow (ctx , fmt .Sprintf ("SELECT 1 FROM pg_roles WHERE rolname='%s';" , username )).Scan (& result )
225
276
if err != nil {
226
277
if err == pgx .ErrNoRows {
227
278
return false , nil
0 commit comments