DELETE: /node/{id}
(id string, ctx context.Context)
| 15 | |
| 16 | // DELETE: /node/{id} |
| 17 | func (cr *NodeController) Delete(id string, ctx context.Context) error { |
| 18 | u, err := request.Authenticate(ctx.HttpRequest()) |
| 19 | if err != nil && err.Error() != e.NoAuth { |
| 20 | return request.AuthError(err, ctx) |
| 21 | } |
| 22 | |
| 23 | // public user (no auth) can be used in some cases |
| 24 | if u == nil { |
| 25 | if conf.ANON_DELETE { |
| 26 | u = &user.User{Uuid: "public"} |
| 27 | } else { |
| 28 | return responder.RespondWithError(ctx, http.StatusUnauthorized, e.NoAuth) |
| 29 | } |
| 30 | } |
| 31 | |
| 32 | // Load node by id |
| 33 | n, err := node.Load(id) |
| 34 | if err != nil { |
| 35 | if err == mgo.ErrNotFound { |
| 36 | logger.Error("err@node_Delete: (node.Load) id=" + id + ": " + e.NodeNotFound) |
| 37 | return responder.RespondWithError(ctx, http.StatusNotFound, e.NodeNotFound) |
| 38 | } else { |
| 39 | // In theory the db connection could be lost between |
| 40 | // checking user and load but seems unlikely. |
| 41 | err_msg := "err@node_Delete: (node.Load) id=" + id + ": " + err.Error() |
| 42 | logger.Error(err_msg) |
| 43 | return responder.RespondWithError(ctx, http.StatusInternalServerError, err_msg) |
| 44 | } |
| 45 | } |
| 46 | |
| 47 | rights := n.Acl.Check(u.Uuid) |
| 48 | prights := n.Acl.Check("public") |
| 49 | if rights["delete"] == false && u.Admin == false && n.Acl.Owner != u.Uuid && prights["delete"] == false { |
| 50 | logger.Error("err@node_Delete: (Authenticate) id=" + id + ": " + e.UnAuth) |
| 51 | return responder.RespondWithError(ctx, http.StatusUnauthorized, e.UnAuth) |
| 52 | } |
| 53 | |
| 54 | if err := n.Delete(); err == nil { |
| 55 | return responder.RespondOK(ctx) |
| 56 | } else { |
| 57 | err_msg := "err@node_Delete: (node.Delete) " + err.Error() |
| 58 | logger.Error(err_msg) |
| 59 | return responder.RespondWithError(ctx, http.StatusInternalServerError, err_msg) |
| 60 | } |
| 61 | } |
no test coverage detected