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