作者 郑周

角色删除

@@ -92,47 +92,53 @@ func NewRoleService() *RoleService { @@ -92,47 +92,53 @@ func NewRoleService() *RoleService {
92 // return role, nil 92 // return role, nil
93 //} 93 //}
94 // 94 //
95 -//func (rs *RoleService) Remove(in *command.DeleteRoleCommand) (interface{}, error) {  
96 -// transactionContext, err := factory.ValidateStartTransaction(in)  
97 -// if err != nil {  
98 -// return nil, err  
99 -// }  
100 -// defer func() {  
101 -// transactionContext.RollbackTransaction()  
102 -// }()  
103 -//  
104 -// roleRepository := factory.CreateRoleRepository(map[string]interface{}{"transactionContext": transactionContext})  
105 -// roleUserRepository := factory.CreateRoleUserRepository(map[string]interface{}{"transactionContext": transactionContext})  
106 -//  
107 -// role, err := roleRepository.FindOne(map[string]interface{}{"id": in.Id})  
108 -// if err != nil {  
109 -// return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, err.Error())  
110 -// }  
111 -// if _, err := roleRepository.Remove(role); err != nil {  
112 -// return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, err.Error())  
113 -// }  
114 -//  
115 -// // 获取角色所有关联的用户,并删除  
116 -// _, roleUsers, err := roleUserRepository.Find(map[string]interface{}{"roleId": in.Id, "companyId": in.CompanyId})  
117 -// if err != nil {  
118 -// return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, err.Error())  
119 -// }  
120 -// ids := make([]int64, 0)  
121 -// for i := range roleUsers {  
122 -// ids = append(ids, roleUsers[i].Id)  
123 -// }  
124 -// if len(ids) > 0 {  
125 -// err := roleUserRepository.BatchDeleteById(ids)  
126 -// if err != nil {  
127 -// return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, err.Error())  
128 -// }  
129 -// }  
130 -//  
131 -// if err := transactionContext.CommitTransaction(); err != nil {  
132 -// return nil, application.ThrowError(application.TRANSACTION_ERROR, err.Error())  
133 -// }  
134 -// return role, nil  
135 -//} 95 +
  96 +func (rs *RoleService) Remove(in *command.DeleteRoleCommand) (interface{}, error) {
  97 + transactionContext, err := factory.ValidateStartTransaction(in)
  98 + if err != nil {
  99 + return nil, err
  100 + }
  101 + defer func() {
  102 + transactionContext.RollbackTransaction()
  103 + }()
  104 +
  105 + roleRepository := factory.CreateRoleRepository(map[string]interface{}{"transactionContext": transactionContext})
  106 + roleUserRepository := factory.CreateRoleUserRepository(map[string]interface{}{"transactionContext": transactionContext})
  107 +
  108 + role, err := roleRepository.FindOne(map[string]interface{}{"id": in.Id})
  109 + if err != nil {
  110 + return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, err.Error())
  111 + }
  112 +
  113 + if role.Type == domain.RoleTypeSystem {
  114 + return nil, application.ThrowError(application.BUSINESS_ERROR, "系统预制角色不可删除")
  115 + }
  116 +
  117 + if _, err := roleRepository.Remove(role); err != nil {
  118 + return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, err.Error())
  119 + }
  120 +
  121 + // 获取角色所有关联的用户,并删除
  122 + _, roleUsers, err := roleUserRepository.Find(map[string]interface{}{"roleId": in.Id, "companyId": in.CompanyId})
  123 + if err != nil {
  124 + return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, err.Error())
  125 + }
  126 + ids := make([]int64, 0)
  127 + for i := range roleUsers {
  128 + ids = append(ids, roleUsers[i].Id)
  129 + }
  130 + if len(ids) > 0 {
  131 + err := roleUserRepository.BatchDeleteById(ids)
  132 + if err != nil {
  133 + return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, err.Error())
  134 + }
  135 + }
  136 +
  137 + if err := transactionContext.CommitTransaction(); err != nil {
  138 + return nil, application.ThrowError(application.TRANSACTION_ERROR, err.Error())
  139 + }
  140 + return role, nil
  141 +}
136 142
137 func (rs *RoleService) ListForUser(in *command.QueryRoleUserCommand) (interface{}, error) { 143 func (rs *RoleService) ListForUser(in *command.QueryRoleUserCommand) (interface{}, error) {
138 transactionContext, err := factory.StartTransaction() 144 transactionContext, err := factory.StartTransaction()
@@ -12,7 +12,7 @@ type RoleController struct { @@ -12,7 +12,7 @@ type RoleController struct {
12 beego.BaseController 12 beego.BaseController
13 } 13 }
14 14
15 -//func (controller *RoleController) CreateRole() { 15 +// func (controller *RoleController) CreateRole() {
16 // ruService := service.NewRoleService() 16 // ruService := service.NewRoleService()
17 // in := &command.CreateRoleCommand{} 17 // in := &command.CreateRoleCommand{}
18 // if err := controller.Unmarshal(in); err != nil { 18 // if err := controller.Unmarshal(in); err != nil {
@@ -21,9 +21,9 @@ type RoleController struct { @@ -21,9 +21,9 @@ type RoleController struct {
21 // in.CompanyId = middlewares.GetCompanyId(controller.Ctx) 21 // in.CompanyId = middlewares.GetCompanyId(controller.Ctx)
22 // controller.Response(ruService.Create(in)) 22 // controller.Response(ruService.Create(in))
23 // } 23 // }
24 -//} 24 +// }
25 // 25 //
26 -//func (controller *RoleController) UpdateRole() { 26 +// func (controller *RoleController) UpdateRole() {
27 // ruService := service.NewRoleService() 27 // ruService := service.NewRoleService()
28 // in := &command.UpdateRoleCommand{} 28 // in := &command.UpdateRoleCommand{}
29 // if err := controller.Unmarshal(in); err != nil { 29 // if err := controller.Unmarshal(in); err != nil {
@@ -32,18 +32,18 @@ type RoleController struct { @@ -32,18 +32,18 @@ type RoleController struct {
32 // in.CompanyId = middlewares.GetCompanyId(controller.Ctx) 32 // in.CompanyId = middlewares.GetCompanyId(controller.Ctx)
33 // controller.Response(ruService.Update(in)) 33 // controller.Response(ruService.Update(in))
34 // } 34 // }
35 -//}  
36 -//  
37 -//func (controller *RoleController) RemoveRole() {  
38 -// ruService := service.NewRoleService()  
39 -// in := &command.DeleteRoleCommand{}  
40 -// if err := controller.Unmarshal(in); err != nil {  
41 -// controller.Response(nil, application.ThrowError(application.ARG_ERROR, err.Error()))  
42 -// } else {  
43 -// in.CompanyId = middlewares.GetCompanyId(controller.Ctx)  
44 -// controller.Response(ruService.Remove(in))  
45 // } 35 // }
46 -//} 36 +func (controller *RoleController) RemoveRole() {
  37 + ruService := service.NewRoleService()
  38 + in := &command.DeleteRoleCommand{}
  39 + if err := controller.Unmarshal(in); err != nil {
  40 + controller.Response(nil, application.ThrowError(application.ARG_ERROR, err.Error()))
  41 + } else {
  42 + ua := middlewares.GetUser(controller.Ctx)
  43 + in.CompanyId = ua.CompanyId
  44 + controller.Response(ruService.Remove(in))
  45 + }
  46 +}
47 47
48 func (controller *RoleController) ListForUserRole() { 48 func (controller *RoleController) ListForUserRole() {
49 ruService := service.NewRoleService() 49 ruService := service.NewRoleService()
@@ -53,7 +53,6 @@ func (controller *RoleController) ListForUserRole() { @@ -53,7 +53,6 @@ func (controller *RoleController) ListForUserRole() {
53 } else { 53 } else {
54 ua := middlewares.GetUser(controller.Ctx) 54 ua := middlewares.GetUser(controller.Ctx)
55 in.CompanyId = ua.CompanyId 55 in.CompanyId = ua.CompanyId
56 - //in.CompanyId = middlewares.GetCompanyId(controller.Ctx)  
57 controller.Response(ruService.ListForUser(in)) 56 controller.Response(ruService.ListForUser(in))
58 } 57 }
59 } 58 }
@@ -12,7 +12,7 @@ func init() { @@ -12,7 +12,7 @@ func init() {
12 web.NSBefore(filters.AllowCors(), middlewares.CheckAdminToken()), 12 web.NSBefore(filters.AllowCors(), middlewares.CheckAdminToken()),
13 //web.NSRouter("/", &controllers.RoleController{}, "Post:CreateRole"), 13 //web.NSRouter("/", &controllers.RoleController{}, "Post:CreateRole"),
14 //web.NSRouter("/", &controllers.RoleController{}, "Put:UpdateRole"), 14 //web.NSRouter("/", &controllers.RoleController{}, "Put:UpdateRole"),
15 - //web.NSRouter("/", &controllers.RoleController{}, "Delete:RemoveRole"), 15 + web.NSRouter("/", &controllers.RoleController{}, "Delete:RemoveRole"),
16 //web.NSRouter("/:Id", &controllers.RoleController{}, "Get:GetRole"), 16 //web.NSRouter("/:Id", &controllers.RoleController{}, "Get:GetRole"),
17 web.NSRouter("/all", &controllers.RoleController{}, "Post:ListForUserRole"), 17 web.NSRouter("/all", &controllers.RoleController{}, "Post:ListForUserRole"),
18 ) 18 )