package role import ( base_ctl "SCRM/controllers" "SCRM/enums" "SCRM/service/role_service" "time" "github.com/astaxie/beego" ) func RoleCtlRegistRouters() { beego.Router("/api/roles", &RoleAPIController{}, "get:GetRoles") beego.Router("/api/role/create", &RoleAPIController{}, "post:CreateRole") beego.Router("/api/role/modify", &RoleAPIController{}, "post:ModifyRole") beego.Router("/api/role/setstatus", &RoleAPIController{}, "post:ModifyRoleStatus") beego.Router("/role/purview/editinit", &RoleAPIController{}, "get:EditPurviewInitData") beego.Router("/role/purview/edit", &RoleAPIController{}, "post:EditPurview") } type RoleAPIController struct { base_ctl.BaseAuthAPIController } // /api/roles [get] // @param page?:int func (this *RoleAPIController) GetRoles() { page, _ := this.GetInt("page") adminUserInfo := this.GetAdminUserInfo() //beego.Alert(adminUserInfo.AdminUser) if adminUserInfo.AdminUser.IsSuperAdmin == false { this.ServeFailJSONWithSGJErrorCode(enums.ErrorCodePermissionDenied) return } if page <= 0 { page = 1 } roles, total, getRoleErr := role_service.GetRoles(adminUserInfo.CurrentOrgId, adminUserInfo.CurrentAppId, page, 10) if getRoleErr != nil { //beego.Error("获取角色列表失败:", getRoleErr) this.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeDataException) } else { this.ServeSuccessJSON(map[string]interface{}{ "roles": roles, "total_count": total, }) } } // /api/role/create [post] // @param name:string // @param intro:string func (this *RoleAPIController) CreateRole() { name := this.GetString("name") intro := this.GetString("intro") if len(name) == 0 || len(intro) == 0 { this.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeParamWrong) return } adminUserInfo := this.GetAdminUserInfo() if adminUserInfo.AdminUser.IsSuperAdmin == false { this.ServeFailJSONWithSGJErrorCode(enums.ErrorCodePermissionDenied) return } role, createErr := role_service.CreateRole(adminUserInfo.AdminUser.Id, adminUserInfo.CurrentOrgId, adminUserInfo.CurrentAppId, name, intro) if createErr != nil { //beego.Error("创建角色失败:", createErr) this.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeDBCreate) } else { this.ServeSuccessJSON(map[string]interface{}{ "id": role.Id, "name": role.RoleName, "intro": role.RoleIntro, "status": role.Status, }) } } // /api/role/modify // @param role_id:int // @param name:string // @param intro:string func (this *RoleAPIController) ModifyRole() { roleID, _ := this.GetInt64("role_id") name := this.GetString("name") intro := this.GetString("intro") if roleID <= 0 || len(name) == 0 || len(intro) == 0 { this.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeParamWrong) return } adminUserInfo := this.GetAdminUserInfo() if adminUserInfo.AdminUser.IsSuperAdmin == false { this.ServeFailJSONWithSGJErrorCode(enums.ErrorCodePermissionDenied) return } role, getRoleErr := role_service.GetRoleByRoleID(roleID) if getRoleErr != nil { //beego.Error("获取角色失败:", getRoleErr) this.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeDataException) return } else if role == nil { this.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeRoleNotExist) return } role.RoleName = name role.RoleIntro = intro role.ModifyTime = time.Now().Unix() saveErr := role_service.ModifyRole(role) if saveErr != nil { //beego.Error("修改角色失败:", role.Id, saveErr) this.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeDBUpdate) } else { this.ServeSuccessJSON(nil) } } // /api/role/setstatus // @param role_id:int // @param enable:bool func (this *RoleAPIController) ModifyRoleStatus() { roleID, _ := this.GetInt64("role_id") enable, _ := this.GetBool("enable") if roleID <= 0 { this.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeParamWrong) return } adminUserInfo := this.GetAdminUserInfo() if adminUserInfo.AdminUser.IsSuperAdmin == false { this.ServeFailJSONWithSGJErrorCode(enums.ErrorCodePermissionDenied) return } role, getRoleErr := role_service.GetRoleByRoleID(roleID) if getRoleErr != nil { //beego.Error("获取角色失败:", getRoleErr) this.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeDataException) return } else if role == nil { this.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeRoleNotExist) return } if enable == false { if count, _ := role_service.RoleAdminUserCount(adminUserInfo.CurrentOrgId, adminUserInfo.CurrentAppId, roleID); count != 0 { this.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeCannotRemoveRole) return } } if enable { role.Status = 1 } else { role.Status = 2 } role.ModifyTime = time.Now().Unix() saveErr := role_service.ModifyRole(role) if saveErr != nil { //beego.Error("修改角色失败:", role.Id, saveErr) this.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeDBUpdate) } else { this.ServeSuccessJSON(nil) } } // /role/purview/editinit [get] // @param role_id:int func (this *RoleAPIController) EditPurviewInitData() { adminUserInfo := this.GetAdminUserInfo() if adminUserInfo.AdminUser.IsSuperAdmin == false { this.ServeFailJSONWithSGJErrorCode(enums.ErrorCodePermissionDenied) return } roleId, _ := this.GetInt64("role_id") if roleId <= 0 { this.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeParamWrong) return } purviews, getPurviewsErr := role_service.GetAllGeneralPurviewVMsProcessed() if getPurviewsErr != nil { //beego.Error("获取所有权限时出错:", getPurviewsErr) this.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeDataException) return } rolePurviewIdStr, getRPIdsErr := role_service.GetRolePurviewIds(roleId) if getRPIdsErr != nil { //beego.Error("获取角色的权限时出错:", getRPIdsErr) this.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeDataException) return } this.ServeSuccessJSON(map[string]interface{}{ "purviews": purviews, "role_purview_ids": rolePurviewIdStr, }) } // /role/purview/edit [post] // @param role_id:int // @param purview_ids:string func (this *RoleAPIController) EditPurview() { adminUserInfo := this.GetAdminUserInfo() if adminUserInfo.AdminUser.IsSuperAdmin == false { this.ServeFailJSONWithSGJErrorCode(enums.ErrorCodePermissionDenied) return } roleId, _ := this.GetInt64("role_id") purviewIds := this.GetString("purview_ids") if roleId <= 0 { this.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeParamWrong) return } err := role_service.SaveRolePurviewIds(adminUserInfo.CurrentOrgId, adminUserInfo.CurrentAppId, roleId, purviewIds) if err != nil { //beego.Error("设置角色的权限时出错:", err) this.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeDBUpdate) } else { this.ServeSuccessJSON(nil) } }