/
destroy.go
executable file
·48 lines (37 loc) · 1012 Bytes
/
destroy.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
package useractions
import (
"net/http"
"github.com/fragmenta/auth/can"
"github.com/fragmenta/mux"
"github.com/fragmenta/server"
"github.com/fragmenta/fragmenta-cms/src/lib/session"
"github.com/fragmenta/fragmenta-cms/src/users"
)
// HandleDestroy responds to /users/n/destroy by deleting the user.
func HandleDestroy(w http.ResponseWriter, r *http.Request) error {
// Get the user params for id
params, err := mux.Params(r)
if err != nil {
return server.InternalError(err)
}
// Find the user
user, err := users.Find(params.GetInt(users.KeyName))
if err != nil {
return server.NotFoundError(err)
}
// Check the authenticity token
err = session.CheckAuthenticity(w, r)
if err != nil {
return err
}
// Authorise destroy user
currentUser := session.CurrentUser(w, r)
err = can.Destroy(user, currentUser)
if err != nil {
return server.NotAuthorizedError(err)
}
// Destroy the user
user.Destroy()
// Redirect to users root
return server.Redirect(w, r, user.IndexURL())
}