|
1 | 1 | package controllers
|
2 | 2 |
|
3 | 3 | import (
|
4 |
| - "encoding/json" |
| 4 | + "fmt" |
5 | 5 | "net/http"
|
6 | 6 | "strconv"
|
7 | 7 |
|
8 |
| - "github.com/gorilla/mux" |
| 8 | + "github.com/astaxie/beego/validation" |
| 9 | + "github.com/jinzhu/gorm" |
9 | 10 | "github.com/laincloud/hagrid/models"
|
10 | 11 | )
|
11 | 12 |
|
12 |
| -func AddAdminHandler(w http.ResponseWriter, r *http.Request) { |
13 |
| - alertID, _ := strconv.Atoi(r.URL.Query().Get("alert_id")) |
14 |
| - var adminID int |
15 |
| - |
16 |
| - _, err := authorize(w, r, alertID) |
17 |
| - if err != nil { |
18 |
| - return |
19 |
| - } |
20 |
| - |
21 |
| - if adminID, err = strconv.Atoi(r.FormValue("adminID")); err != nil { |
22 |
| - writeResponse(w, "Field user_id must be an integer", http.StatusBadRequest) |
23 |
| - return |
24 |
| - } |
25 |
| - |
26 |
| - if models.IsAdminDuplicated(alertID, adminID) { |
27 |
| - writeResponse(w, "The admin is duplicated in this alert", http.StatusConflict) |
28 |
| - return |
29 |
| - } |
30 |
| - |
31 |
| - if err = models.AddAdmin(alertID, adminID); err != nil { |
32 |
| - writeResponse(w, err.Error(), http.StatusInternalServerError) |
33 |
| - return |
34 |
| - } |
35 |
| - writeResponse(w, "Add administrator successfully", http.StatusCreated) |
| 13 | +type AdminController struct { |
| 14 | + AuthController |
36 | 15 | }
|
37 | 16 |
|
38 |
| -func DeleteAdminHandler(w http.ResponseWriter, r *http.Request) { |
39 |
| - |
40 |
| - alertID, _ := strconv.Atoi(r.URL.Query().Get("alert_id")) |
41 |
| - |
42 |
| - user, err := authorize(w, r, alertID) |
43 |
| - if err != nil { |
44 |
| - return |
| 17 | +func (this *AdminController) NestPrepare() { |
| 18 | + var adminID int |
| 19 | + if this.Ctx.Input.IsPost() { |
| 20 | + validator := validation.Validation{} |
| 21 | + adminIDStr := this.GetString("admin_id") |
| 22 | + validator.Required(adminIDStr, "admin_id") |
| 23 | + validator.Numeric(adminIDStr, "admin_id") |
| 24 | + if validator.HasErrors() { |
| 25 | + for _, err := range validator.Errors { |
| 26 | + this.outputError(http.StatusBadRequest, fmt.Sprintf("[%s]%s", err.Key, err.Message)) |
| 27 | + this.ServeJSON() |
| 28 | + this.StopRun() |
| 29 | + } |
| 30 | + } |
| 31 | + adminID, _ = strconv.Atoi(adminIDStr) |
| 32 | + } else { |
| 33 | + adminID, _ = strconv.Atoi(this.Ctx.Input.Param(":admin_id")) |
| 34 | + if this.Ctx.Input.IsDelete() { |
| 35 | + authUser := this.Ctx.Input.GetData("auth_user").(*models.User) |
| 36 | + if authUser.ID == adminID { |
| 37 | + this.outputError(http.StatusForbidden, errorMsg403) |
| 38 | + this.ServeJSON() |
| 39 | + this.StopRun() |
| 40 | + } |
| 41 | + } |
45 | 42 | }
|
46 |
| - |
47 |
| - vars := mux.Vars(r) |
48 |
| - id, _ := strconv.Atoi(vars["id"]) |
49 |
| - |
50 |
| - if id == user.ID { |
51 |
| - writeResponse(w, "You can't delete yourself!", http.StatusForbidden) |
52 |
| - return |
| 43 | + if isSuper, err := models.IsSuperUser(adminID); isSuper { |
| 44 | + this.outputError(http.StatusForbidden, errorMsg403) |
| 45 | + this.ServeJSON() |
| 46 | + this.StopRun() |
| 47 | + } else if err != nil { |
| 48 | + this.outputError(http.StatusInternalServerError, errorMsg500) |
| 49 | + this.ServeJSON() |
| 50 | + this.StopRun() |
| 51 | + } else { |
| 52 | + this.Ctx.Input.SetData("adminID", adminID) |
53 | 53 | }
|
| 54 | +} |
54 | 55 |
|
55 |
| - if err = models.DeleteAdmin(alertID, id); err != nil { |
56 |
| - writeResponse(w, err.Error(), http.StatusForbidden) |
57 |
| - return |
| 56 | +func (this *AdminController) Post() { |
| 57 | + adminID := this.Ctx.Input.GetData("adminID").(int) |
| 58 | + alertID, _ := strconv.Atoi(this.Ctx.Input.Param(":alert_id")) |
| 59 | + if err := models.AddAdmin(alertID, adminID); err == models.ErrorDuplicatedName { |
| 60 | + this.outputError(http.StatusConflict, errorMsg409) |
| 61 | + } else if err == gorm.ErrRecordNotFound { |
| 62 | + this.outputError(http.StatusNotFound, errorMsg404) |
| 63 | + } else if err != nil { |
| 64 | + controllerLogger.Printf("PostAdmin failed: %s", err.Error()) |
| 65 | + this.outputError(http.StatusInternalServerError, errorMsg500) |
| 66 | + } else { |
| 67 | + this.outputSuccess(http.StatusCreated, map[string]string{ |
| 68 | + "message": "Add admin successfully", |
| 69 | + }) |
58 | 70 | }
|
59 |
| - writeResponse(w, "Delete administrator successfully", http.StatusNoContent) |
| 71 | + this.ServeJSON() |
60 | 72 | }
|
61 | 73 |
|
62 |
| -func GetAdminsHandler(w http.ResponseWriter, r *http.Request) { |
63 |
| - alertID, _ := strconv.Atoi(r.URL.Query().Get("alert_id")) |
64 |
| - var ( |
65 |
| - admins []models.User |
66 |
| - err error |
67 |
| - data []byte |
68 |
| - ) |
69 |
| - |
70 |
| - if err = models.GetAdminsByAlertID(alertID, &admins); err != nil { |
71 |
| - writeResponse(w, err.Error(), http.StatusInternalServerError) |
72 |
| - return |
| 74 | +func (this *AdminController) Delete() { |
| 75 | + adminID := this.Ctx.Input.GetData("adminID").(int) |
| 76 | + alertID, _ := strconv.Atoi(this.Ctx.Input.Param(":alert_id")) |
| 77 | + if err := models.DeleteAdmin(alertID, adminID); err == gorm.ErrRecordNotFound { |
| 78 | + this.outputError(http.StatusNotFound, errorMsg404) |
| 79 | + } else if err != nil { |
| 80 | + controllerLogger.Printf("DeleteAdmin failed: %s", err.Error()) |
| 81 | + this.outputError(http.StatusInternalServerError, errorMsg500) |
| 82 | + } else { |
| 83 | + this.outputSuccess(http.StatusNoContent, succMsg204) |
73 | 84 | }
|
| 85 | + this.ServeJSON() |
| 86 | +} |
74 | 87 |
|
75 |
| - briefUsers := make([]UserMV, 0, len(admins)) |
76 |
| - for _, user := range admins { |
77 |
| - briefUsers = append( |
78 |
| - briefUsers, |
79 |
| - UserMV{ |
80 |
| - ID: user.ID, |
81 |
| - Name: user.Name, |
82 |
| - }, |
83 |
| - ) |
84 |
| - } |
85 |
| - if data, err = json.Marshal(briefUsers); err != nil { |
86 |
| - writeResponse(w, err.Error(), http.StatusInternalServerError) |
87 |
| - return |
| 88 | +func (this *AdminController) Get() { |
| 89 | + alertID, _ := strconv.Atoi(this.Ctx.Input.Param(":alert_id")) |
| 90 | + var admins []models.User |
| 91 | + if err := models.GetAdminsByAlertID(alertID, &admins); err == gorm.ErrRecordNotFound { |
| 92 | + this.outputError(http.StatusNotFound, errorMsg404) |
| 93 | + } else if err != nil { |
| 94 | + controllerLogger.Printf("GetAdmins failed: %s", err.Error()) |
| 95 | + this.outputError(http.StatusInternalServerError, errorMsg500) |
| 96 | + } else { |
| 97 | + this.outputSuccess(http.StatusOK, &admins) |
88 | 98 | }
|
89 |
| - w.WriteHeader(http.StatusOK) |
90 |
| - w.Write(data) |
| 99 | + this.ServeJSON() |
91 | 100 | }
|
0 commit comments