Mercurial > gemma
comparison pkg/controllers/user.go @ 5504:5e3e3d9e2c23 deactivate-users
Removed delete_user plpgsql function.
author | Sascha L. Teichmann <sascha.teichmann@intevation.de> |
---|---|
date | Wed, 22 Sep 2021 18:50:16 +0200 |
parents | f0c668bc4082 |
children | 2604beecbd1a |
comparison
equal
deleted
inserted
replaced
5503:076b6b17c4a9 | 5504:5e3e3d9e2c23 |
---|---|
55 updateUserExtentSQL = `UPDATE users.list_users | 55 updateUserExtentSQL = `UPDATE users.list_users |
56 SET (rolname, username, pw, country, map_extent, email_address, report_reciever) | 56 SET (rolname, username, pw, country, map_extent, email_address, report_reciever) |
57 = ($2, $3, $4, $5, ST_MakeBox2D(ST_Point($6, $7), ST_Point($8, $9)), $10, $11) | 57 = ($2, $3, $4, $5, ST_MakeBox2D(ST_Point($6, $7), ST_Point($8, $9)), $10, $11) |
58 WHERE username = $1` | 58 WHERE username = $1` |
59 | 59 |
60 deleteUserSQL = `SELECT delete_user($1)` | 60 deleteUserSQL = ` |
61 WITH del AS ( | |
62 DELETE FROM users.list_users | |
63 WHERE username = $1 AND ( | |
64 rolname NOT IN ('waterway_admin', 'sys_admin') | |
65 OR NOT EXISTS (SELECT 1 FROM import.imports WHERE username = $1)) | |
66 RETURNING * | |
67 ), | |
68 up AS ( | |
69 UPDATE users.list_users | |
70 SET (email_address, report_reciever, active) = ('nomail@example.com', false, false) | |
71 WHERE username = $1 AND NOT EXISTS (SELECT * FROM del) | |
72 RETURNING * | |
73 ) | |
74 SELECT | |
75 EXISTS (SELECT * FROM del) AS deleted, | |
76 EXISTS (SELECT * FROM up) AS updated` | |
61 | 77 |
62 listUsersSQL = `SELECT | 78 listUsersSQL = `SELECT |
63 rolname, | 79 rolname, |
64 username, | 80 username, |
65 country, | 81 country, |
143 } | 159 } |
144 } else { | 160 } else { |
145 log.Errorf("%v\n", err2) | 161 log.Errorf("%v\n", err2) |
146 } | 162 } |
147 | 163 |
148 var result int64 | 164 var deleted, updated bool |
149 if err = db.QueryRowContext(ctx, deleteUserSQL, user).Scan(&result); err != nil { | 165 if err = db.QueryRowContext(ctx, deleteUserSQL, user).Scan(&deleted, &updated); err != nil { |
150 return | 166 return |
151 } | 167 } |
152 | 168 |
153 switch result { | 169 var action string |
154 case 0: | 170 |
171 switch { | |
172 case !deleted && !updated: | |
155 err = mw.JSONError{ | 173 err = mw.JSONError{ |
156 Code: http.StatusNotFound, | 174 Code: http.StatusNotFound, |
157 Message: fmt.Sprintf("Cannot find user %s.", user), | 175 Message: fmt.Sprintf("Cannot find user %s.", user), |
158 } | 176 } |
159 return | 177 return |
160 case 1: | 178 case deleted: |
161 log.Debugf("user '%s' was deleted.\n", user) | 179 log.Debugf("user '%s' was deleted.\n", user) |
162 case 2: | 180 action = "deleted" |
181 case updated: | |
163 log.Debugf("user '%s' was deactivated.\n", user) | 182 log.Debugf("user '%s' was deactivated.\n", user) |
183 action = "deactivated" | |
184 default: | |
185 log.Errorf("Should not happen: user '%s' updated and deleted.\n", user) | |
186 action = "deleted" | |
164 } | 187 } |
165 | 188 |
166 // Running in a go routine should not be necessary. | 189 // Running in a go routine should not be necessary. |
167 go func() { auth.Sessions.Logout(user) }() | 190 go func() { auth.Sessions.Logout(user) }() |
168 | 191 |
169 jr = mw.JSONResult{Code: http.StatusNoContent} | 192 jr = mw.JSONResult{ |
193 Result: struct { | |
194 Action string `json:"action"` | |
195 }{ | |
196 Action: action, | |
197 }, | |
198 } | |
170 return | 199 return |
171 } | 200 } |
172 | 201 |
173 func updateUser(req *http.Request) (jr mw.JSONResult, err error) { | 202 func updateUser(req *http.Request) (jr mw.JSONResult, err error) { |
174 | 203 |