Skip to content

Commit 2698c8b

Browse files
committed
delete user
1 parent 697fb6d commit 2698c8b

File tree

6 files changed

+64
-11
lines changed

6 files changed

+64
-11
lines changed

app-server.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ const usage = `
2020
Postgres-CI app-server
2121
2222
Usage:
23-
-c /path/to/config.yaml (if empty app will use environment variables)
23+
-c /path/to/config.yaml (if not setted app will use environment variables)
2424
-debug (enable debug mode)
2525
2626
Environment variables:

assets/static/js/app.js

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,13 @@
11
$('#confirmAction').on('show.bs.modal', function (event) {
22

3-
var button = $(event.relatedTarget);
4-
5-
$(this).find('.modal-content form').attr('action', button.data('action'));
3+
$(this).find('.modal-content form').attr('action', $(event.relatedTarget).data('action'));
4+
5+
}).on('hide.bs.modal', function (event) {
6+
7+
$(this).find('.modal-content form input[type=password]').val('');
68
});
79

10+
811
$('#confirmAction').find('.modal-content form').on('submit', function(event) {
912

1013
$.ajax({
@@ -18,9 +21,12 @@ $('#confirmAction').find('.modal-content form').on('submit', function(event) {
1821
error: function(request, status, error) {
1922

2023
switch (request.status) {
21-
case 403:
24+
case 401:
2225
alert("Authentication failed");
2326
break;
27+
case 403:
28+
alert("Access denied");
29+
break;
2430
default:
2531
alert(error);
2632
break;
@@ -29,4 +35,4 @@ $('#confirmAction').find('.modal-content form').on('submit', function(event) {
2935
});
3036

3137
return false
32-
});
38+
});

src/app/controllers/users/delete.go

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
package users
2+
3+
import (
4+
"github.com/postgres-ci/app-server/src/app/models/auth"
5+
"github.com/postgres-ci/app-server/src/app/models/users"
6+
"github.com/postgres-ci/app-server/src/tools/params"
7+
"github.com/postgres-ci/app-server/src/tools/render"
8+
"github.com/postgres-ci/http200ok"
9+
10+
"net/http"
11+
)
12+
13+
func deleteHandler(c *http200ok.Context) {
14+
15+
currentUser := c.Get("CurrentUser").(*auth.User)
16+
17+
if !currentUser.IsSuperuser {
18+
19+
render.JSONError(c, http.StatusForbidden, "Access denied")
20+
21+
return
22+
}
23+
24+
if err := users.Delete(params.ToInt32(c, "UserID")); err != nil {
25+
26+
render.JSONError(c, http.StatusInternalServerError, err.Error())
27+
28+
return
29+
}
30+
31+
render.JSONok(c)
32+
}

src/app/controllers/users/route.go

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,10 @@ package users
22

33
import (
44
"github.com/postgres-ci/app-server/src/app/middleware"
5-
"github.com/postgres-ci/app-server/src/tools/render"
65
"github.com/postgres-ci/http200ok"
76
)
87

98
func Route(server *http200ok.Server) {
109
server.Get("/users/", listHandler)
11-
server.Post("/users/delete/:UserID/", middleware.CheckPassword, func(c *http200ok.Context) {
12-
render.JSONok(c)
13-
})
10+
server.Post("/users/delete/:UserID/", middleware.CheckPassword, deleteHandler)
1411
}

src/app/middleware/check_password.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ func CheckPassword(c *http200ok.Context) {
1919
}
2020
}
2121

22-
render.JSONError(c, http.StatusForbidden, "Authentication failed")
22+
render.JSONError(c, http.StatusUnauthorized, "Authentication failed")
2323

2424
c.Stop()
2525
}

src/app/models/users/delete.go

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
package users
2+
3+
import (
4+
log "github.com/Sirupsen/logrus"
5+
"github.com/postgres-ci/app-server/src/env"
6+
)
7+
8+
func Delete(userID int32) error {
9+
10+
if _, err := env.Connect().Exec("SELECT users.delete($1)", userID); err != nil {
11+
12+
log.Errorf("Could not delete user %d, err: %v", userID, err)
13+
14+
return err
15+
}
16+
17+
return nil
18+
}

0 commit comments

Comments
 (0)