func (this *RoleController) GetOne() { idStr := this.Ctx.Input.Params[":id"] id, err := strconv.ParseInt(idStr, 0, 64) if err != nil { beego.Debug("ParseRoleId:", err) this.RetError(errInputData) return } role := models.Role{} if code, err := role.FindById(id); err != nil { beego.Error("FindRoleById:", err) if code == models.ErrNotFound { this.RetError(errNoUser) } else { this.RetError(errDatabase) } return } beego.Debug("RoleInfo:", &role) role.ClearPass() this.Data["json"] = &models.RoleGetOneInfo{RoleInfo: &role} this.ServeJson() }
func (this *RoleController) Delete() { token, e := this.ParseToken() if e != nil { this.RetError(e) return } if token.Claims["id"] != "1" { this.RetError(errPermission) return } idStr := this.Ctx.Input.Params[":id"] id, err := strconv.ParseInt(idStr, 0, 64) if err != nil { beego.Debug("ParseRoleId:", err) this.RetError(errInputData) return } role := models.Role{} if code, err := role.DeleteById(id); err != nil { beego.Error("DeleteRoleById:", err) this.RetError(errDatabase) return } else if code == models.ErrNotFound { this.RetError(errNoUser) return } }
func (this *RoleController) Auth() { form := models.RoleAuthForm{} err := json.Unmarshal(this.Ctx.Input.RequestBody, &form) if err != nil { beego.Debug("ParseRoleAuth:", err) this.RetError(errInputData) return } beego.Debug("ParseRoleAuth:", &form) role := models.Role{} if code, err := role.FindById(form.Id); err != nil { beego.Error("FindRoleById:", err) if code == models.ErrNotFound { this.RetError(errNoUser) } else { this.RetError(errDatabase) } return } beego.Debug("RoleInfo:", &role) if role.Name != form.Name || role.Password != form.Password { this.RetError(errPass) return } // Create the token token := jwt.New(jwt.SigningMethodHS256) // Set some claims token.Claims["id"] = strconv.FormatInt(form.Id, 10) token.Claims["name"] = form.Name token.Claims["exp"] = time.Now().Add(time.Hour * 24).Unix() // Sign and get the complete encoded token as a string tokenString, err := token.SignedString([]byte("secret")) if err != nil { beego.Error("jwt.SignedString:", err) this.RetError(errSystem) return } this.Data["json"] = &models.RoleAuthInfo{Token: tokenString} this.ServeJson() }
func (this *RoleController) Put() { token, e := this.ParseToken() if e != nil { this.RetError(e) return } idStr := this.Ctx.Input.Params[":id"] if token.Claims["id"] != idStr && token.Claims["id"] != "1" { this.RetError(errPermission) return } id, err := strconv.ParseInt(idStr, 0, 64) if err != nil { beego.Debug("ParseRoleId:", err) this.RetError(errInputData) return } form := models.RolePutForm{} err = json.Unmarshal(this.Ctx.Input.RequestBody, &form) if err != nil { beego.Debug("ParseRolePut:", err) this.RetError(errInputData) return } beego.Debug("ParseRolePut:", &form) role := models.Role{} if code, err := role.UpdateById(id, &form); err != nil { beego.Error("UpdateRoleById:", err) this.RetError(errDatabase) return } else if code == models.ErrNotFound { this.RetError(errNoUserChange) return } if code, err := role.FindById(id); err != nil { beego.Error("FindRoleById:", err) if code == models.ErrNotFound { this.RetError(errNoUser) } else { this.RetError(errDatabase) } return } beego.Debug("NewRoleInfo:", &role) role.ClearPass() this.Data["json"] = &models.RolePutInfo{RoleInfo: &role} this.ServeJson() }