作者 唐旭辉

角管理相关

1 ### 接口完成进度 1 ### 接口完成进度
2 2
3 | 功能 | 完成状态 | 时间 | 接口路径 | 请求方式 | 3 | 功能 | 完成状态 | 时间 | 接口路径 | 请求方式 |
4 -| -------------- | ------------------ | -------------- | -------------------------- | -------- | 4 +| -------------- | ------------------ | --------------- | -------------------------- | -------- |
5 | 登录 | 进行中 | | | 5 | 登录 | 进行中 | | |
6 | 短信验证码 | 未开始 | | | 6 | 短信验证码 | 未开始 | | |
7 | 获取 token | 未开始 | | | 7 | 获取 token | 未开始 | | |
8 | 刷新 token | 未开始 | | | 8 | 刷新 token | 未开始 | | |
9 | 添加角色 | 完成 | 2019.11.26 | /v1/rbac/role | post | 9 | 添加角色 | 完成 | 2019.11.26 | /v1/rbac/role | post |
10 | 编辑角色 | 完成 | 2019.11.26 | /v1/rbac/role | put | 10 | 编辑角色 | 完成 | 2019.11.26 | /v1/rbac/role | put |
11 -| 删除角色 | 完成 | 2019.11.26 | /v1/rbac/role | delete | 11 +| ~~删除角色~~ | ~~完成(待变更)~~ | ~~ 2019.11.26~~ | /v1/rbac/role | delete |
  12 +| 删除角色 | 完成 | 2019.12.04 | /v1/rbac/role | delete |
  13 +| 添加角色组 | 完成 | 2019.12.04 | /v1/rbac/role_group | post |
  14 +| 编辑角色组 | 完成 | 2019.12.04 | /v1/rbac/role_group | put |
  15 +| 删除角色组 | 完成 | 2019.12.04 | /v1/rbac/role_group | delete |
12 | 添加部门 | 完成 | 2019.11.28 | /v1/company/department | post | 16 | 添加部门 | 完成 | 2019.11.28 | /v1/company/department | post |
13 | 编辑部门 | 完成 | 2019.11.29 | /v1/company/department | put | 17 | 编辑部门 | 完成 | 2019.11.29 | /v1/company/department | put |
14 | ~~删除部门~~ | ~~完成(待变更)~~ | ~~2019.11.29~~ | ~~/v1/company/department~~ | delete | 18 | ~~删除部门~~ | ~~完成(待变更)~~ | ~~2019.11.29~~ | ~~/v1/company/department~~ | delete |
@@ -3,6 +3,7 @@ package controllers @@ -3,6 +3,7 @@ package controllers
3 import ( 3 import (
4 "encoding/json" 4 "encoding/json"
5 "oppmg/common/log" 5 "oppmg/common/log"
  6 + "oppmg/models"
6 "oppmg/protocol" 7 "oppmg/protocol"
7 serverbac "oppmg/services/rbac" 8 serverbac "oppmg/services/rbac"
8 ) 9 )
@@ -17,7 +18,7 @@ func (c *RbacController) URLMapping() { @@ -17,7 +18,7 @@ func (c *RbacController) URLMapping() {
17 } 18 }
18 19
19 // RoleAdd 添加部门 20 // RoleAdd 添加部门
20 -// @router /department [post] 21 +// @router /role [post]
21 func (c *RbacController) RoleAdd() { 22 func (c *RbacController) RoleAdd() {
22 var msg *protocol.ResponseMessage 23 var msg *protocol.ResponseMessage
23 defer func() { 24 defer func() {
@@ -40,7 +41,7 @@ func (c *RbacController) RoleAdd() { @@ -40,7 +41,7 @@ func (c *RbacController) RoleAdd() {
40 } 41 }
41 42
42 // RoleUpdate 编辑部门 43 // RoleUpdate 编辑部门
43 -// @router /department [put] 44 +// @router /role [put]
44 func (c *RbacController) RoleUpdate() { 45 func (c *RbacController) RoleUpdate() {
45 var msg *protocol.ResponseMessage 46 var msg *protocol.ResponseMessage
46 defer func() { 47 defer func() {
@@ -64,7 +65,7 @@ func (c *RbacController) RoleUpdate() { @@ -64,7 +65,7 @@ func (c *RbacController) RoleUpdate() {
64 } 65 }
65 66
66 // RoleDelete 删除角色 67 // RoleDelete 删除角色
67 -// @router /department [delete] 68 +// @router /role[delete]
68 func (c *RbacController) RoleDelete() { 69 func (c *RbacController) RoleDelete() {
69 var msg *protocol.ResponseMessage 70 var msg *protocol.ResponseMessage
70 defer func() { 71 defer func() {
@@ -86,3 +87,78 @@ func (c *RbacController) RoleDelete() { @@ -86,3 +87,78 @@ func (c *RbacController) RoleDelete() {
86 msg = protocol.NewReturnResponse(nil, err) 87 msg = protocol.NewReturnResponse(nil, err)
87 return 88 return
88 } 89 }
  90 +
  91 +// RoleGroupAdd 角色组
  92 +// @router /role_group [post]
  93 +func (c *RbacController) RoleGroupAdd() {
  94 + var msg *protocol.ResponseMessage
  95 + defer func() {
  96 + c.ResposeJson(msg)
  97 + }()
  98 + var param protocol.RequestRoleAdd
  99 + if err := json.Unmarshal(c.Ctx.Input.RequestBody, &param); err != nil {
  100 + log.Error("json 解析失败 err:%s", err)
  101 + msg = protocol.BadRequestParam("1")
  102 + return
  103 + }
  104 + if param.CompanyID <= 0 {
  105 + log.Error("param.CompanyID <= 0")
  106 + msg = protocol.BadRequestParam("1")
  107 + return
  108 + }
  109 + param.Pid = 0
  110 + param.Types = models.ROLETYPES_GROUP
  111 + roleinfo, err := serverbac.RoleAdd(param)
  112 + msg = protocol.NewReturnResponse(roleinfo, err)
  113 + return
  114 +}
  115 +
  116 +// RoleGroupEdit 角色组
  117 +// @router /role_group [put]
  118 +func (c *RbacController) RoleGroupUpdate() {
  119 + var msg *protocol.ResponseMessage
  120 + defer func() {
  121 + c.ResposeJson(msg)
  122 + }()
  123 + var param protocol.RequestRoleEdit
  124 + if err := json.Unmarshal(c.Ctx.Input.RequestBody, &param); err != nil {
  125 + log.Error("json 解析失败 err:%s", err)
  126 + msg = protocol.BadRequestParam("1")
  127 + return
  128 + }
  129 + if param.CompanyID <= 0 {
  130 + log.Error("param.CompanyID <= 0")
  131 + msg = protocol.BadRequestParam("1")
  132 + return
  133 + }
  134 + param.Pid = 0
  135 + param.Types = models.ROLETYPES_GROUP
  136 + roleinfo, err := serverbac.RoleEdit(param)
  137 + msg = protocol.NewReturnResponse(roleinfo, err)
  138 + return
  139 +}
  140 +
  141 +// RoleGroupEdit 角色组
  142 +// @router /role_group [delete]
  143 +func (c *RbacController) RoleGroupDelete() {
  144 + var msg *protocol.ResponseMessage
  145 + defer func() {
  146 + c.ResposeJson(msg)
  147 + }()
  148 + var param protocol.RequestRoleDelete
  149 + if err := json.Unmarshal(c.Ctx.Input.RequestBody, &param); err != nil {
  150 + log.Error("json 解析失败 err:%s", err)
  151 + msg = protocol.BadRequestParam("1")
  152 + return
  153 + }
  154 + if param.CompanyID <= 0 {
  155 + log.Error("param.CompanyID <= 0")
  156 + msg = protocol.BadRequestParam("1")
  157 + return
  158 + }
  159 + err := serverbac.RoleGroupDelete(param)
  160 + msg = protocol.NewReturnResponse(nil, err)
  161 + return
  162 +}
  163 +
  164 +// RoleGroupDelete
@@ -9,11 +9,11 @@ import ( @@ -9,11 +9,11 @@ import (
9 ) 9 )
10 10
11 type Role struct { 11 type Role struct {
12 - Id int `orm:"column(id);auto"` 12 + Id int64 `orm:"column(id);auto"`
13 Name string `orm:"column(name);size(30)"` 13 Name string `orm:"column(name);size(30)"`
14 - Pid int `orm:"column(pid)"` 14 + Pid int64 `orm:"column(pid)"`
15 Types int8 `orm:"column(types)"` 15 Types int8 `orm:"column(types)"`
16 - CompanyId int `orm:"column(company_id)"` 16 + CompanyId int64 `orm:"column(company_id)"`
17 Descript string `orm:"column(descript)"` 17 Descript string `orm:"column(descript)"`
18 CreateAt time.Time `orm:"column(create_at);type(timestamp)"` 18 CreateAt time.Time `orm:"column(create_at);type(timestamp)"`
19 DeleteAt time.Time `orm:"column(delete_at);type(timestamp)"` 19 DeleteAt time.Time `orm:"column(delete_at);type(timestamp)"`
@@ -58,6 +58,9 @@ func (t *Role) ValidatePid() (*Role, error) { @@ -58,6 +58,9 @@ func (t *Role) ValidatePid() (*Role, error) {
58 if err != nil { 58 if err != nil {
59 return nil, err 59 return nil, err
60 } 60 }
  61 + if roledata.DeleteAt.Unix() != 0 {
  62 + return nil, errors.New("roledata.DeleteAt !=0")
  63 + }
61 if roledata.Types != ROLETYPES_GROUP { 64 if roledata.Types != ROLETYPES_GROUP {
62 return nil, errors.New("roledata.Types != ROLETYPES_GROUP") 65 return nil, errors.New("roledata.Types != ROLETYPES_GROUP")
63 } 66 }
@@ -78,7 +81,7 @@ func AddRole(m *Role) (id int64, err error) { @@ -78,7 +81,7 @@ func AddRole(m *Role) (id int64, err error) {
78 81
79 // GetRoleById retrieves Role by Id. Returns error if 82 // GetRoleById retrieves Role by Id. Returns error if
80 // Id doesn't exist 83 // Id doesn't exist
81 -func GetRoleById(id int) (v *Role, err error) { 84 +func GetRoleById(id int64) (v *Role, err error) {
82 o := orm.NewOrm() 85 o := orm.NewOrm()
83 v = &Role{Id: id} 86 v = &Role{Id: id}
84 if err = o.Read(v); err == nil { 87 if err = o.Read(v); err == nil {
@@ -104,7 +107,7 @@ func UpdateRoleById(m *Role, col []string) (err error) { @@ -104,7 +107,7 @@ func UpdateRoleById(m *Role, col []string) (err error) {
104 107
105 // DeleteRole deletes Role by Id and returns error if 108 // DeleteRole deletes Role by Id and returns error if
106 // the record to be deleted doesn't exist 109 // the record to be deleted doesn't exist
107 -func DeleteRoleByID(id int) (err error) { 110 +func DeleteRoleByID(id int64) (err error) {
108 o := orm.NewOrm() 111 o := orm.NewOrm()
109 v := Role{Id: id} 112 v := Role{Id: id}
110 // ascertain id exists in the database 113 // ascertain id exists in the database
@@ -118,7 +121,7 @@ func DeleteRoleByID(id int) (err error) { @@ -118,7 +121,7 @@ func DeleteRoleByID(id int) (err error) {
118 return 121 return
119 } 122 }
120 123
121 -func DeleteRoleByPid(pid int) (err error) { 124 +func DeleteRoleByPid(pid int64) (err error) {
122 o := orm.NewOrm() 125 o := orm.NewOrm()
123 var num int64 126 var num int64
124 deleteAt := time.Now() 127 deleteAt := time.Now()
@@ -128,3 +131,15 @@ func DeleteRoleByPid(pid int) (err error) { @@ -128,3 +131,15 @@ func DeleteRoleByPid(pid int) (err error) {
128 log.Debug("Number of records updated in database:%d", num) 131 log.Debug("Number of records updated in database:%d", num)
129 return 132 return
130 } 133 }
  134 +
  135 +func CountRoleByPid(pid int64) (int64, error) {
  136 + o := orm.NewOrm()
  137 + var (
  138 + cnt int64
  139 + err error
  140 + )
  141 + cnt, err = o.QueryTable(&Role{}).
  142 + Filter("delete_at", 0).
  143 + Filter("pid", pid).Count()
  144 + return cnt, err
  145 +}
  1 +package models
  2 +
  3 +import (
  4 + "fmt"
  5 +
  6 + "github.com/astaxie/beego/orm"
  7 +)
  8 +
  9 +type RoleRelated struct {
  10 + Id int `orm:"column(id);pk"`
  11 + RoleId int64 `orm:"column(role_id)"`
  12 + UserId int64 `orm:"column(user_id)"`
  13 +}
  14 +
  15 +func (t *RoleRelated) TableName() string {
  16 + return "role_related"
  17 +}
  18 +
  19 +func init() {
  20 + orm.RegisterModel(new(RoleRelated))
  21 +}
  22 +
  23 +// AddRoleRelated insert a new RoleRelated into database and returns
  24 +// last inserted Id on success.
  25 +func AddRoleRelated(m *RoleRelated) (id int64, err error) {
  26 + o := orm.NewOrm()
  27 + id, err = o.Insert(m)
  28 + return
  29 +}
  30 +
  31 +// GetRoleRelatedById retrieves RoleRelated by Id. Returns error if
  32 +// Id doesn't exist
  33 +func GetRoleRelatedById(id int) (v *RoleRelated, err error) {
  34 + o := orm.NewOrm()
  35 + v = &RoleRelated{Id: id}
  36 + if err = o.Read(v); err == nil {
  37 + return v, nil
  38 + }
  39 + return nil, err
  40 +}
  41 +
  42 +// UpdateRoleRelated updates RoleRelated by Id and returns error if
  43 +// the record to be updated doesn't exist
  44 +func UpdateRoleRelatedById(m *RoleRelated) (err error) {
  45 + o := orm.NewOrm()
  46 + v := RoleRelated{Id: m.Id}
  47 + // ascertain id exists in the database
  48 + if err = o.Read(&v); err == nil {
  49 + var num int64
  50 + if num, err = o.Update(m); err == nil {
  51 + fmt.Println("Number of records updated in database:", num)
  52 + }
  53 + }
  54 + return
  55 +}
  56 +
  57 +// DeleteRoleRelated deletes RoleRelated by Id and returns error if
  58 +// the record to be deleted doesn't exist
  59 +func DeleteRoleRelated(id int) (err error) {
  60 + o := orm.NewOrm()
  61 + v := RoleRelated{Id: id}
  62 + // ascertain id exists in the database
  63 + if err = o.Read(&v); err == nil {
  64 + var num int64
  65 + if num, err = o.Delete(&RoleRelated{Id: id}); err == nil {
  66 + fmt.Println("Number of records deleted in database:", num)
  67 + }
  68 + }
  69 + return
  70 +}
  71 +
  72 +func CountRoleRelatedByUser(roleid int64) (int64, error) {
  73 + var (
  74 + cnt int64
  75 + err error
  76 + )
  77 + o := orm.NewOrm()
  78 + cnt, err = o.QueryTable(&RoleRelated{}).
  79 + Filter("role_id", roleid).
  80 + Count()
  81 + return cnt, err
  82 +}
1 package models 1 package models
2 2
3 import ( 3 import (
4 - "errors"  
5 "fmt" 4 "fmt"
6 - "reflect"  
7 - "strings"  
8 "time" 5 "time"
9 6
10 "github.com/astaxie/beego/orm" 7 "github.com/astaxie/beego/orm"
@@ -34,8 +31,14 @@ func init() { @@ -34,8 +31,14 @@ func init() {
34 31
35 // AddUser insert a new User into database and returns 32 // AddUser insert a new User into database and returns
36 // last inserted Id on success. 33 // last inserted Id on success.
37 -func AddUser(m *User) (id int64, err error) {  
38 - o := orm.NewOrm() 34 +func AddUser(m *User, om ...orm.Ormer) (id int64, err error) {
  35 + var o orm.Ormer
  36 + if len(om) > 0 {
  37 + o = om[0]
  38 + } else {
  39 + o = orm.NewOrm()
  40 + }
  41 +
39 id, err = o.Insert(m) 42 id, err = o.Insert(m)
40 return 43 return
41 } 44 }
@@ -51,84 +54,6 @@ func GetUserById(id int64) (v *User, err error) { @@ -51,84 +54,6 @@ func GetUserById(id int64) (v *User, err error) {
51 return nil, err 54 return nil, err
52 } 55 }
53 56
54 -// GetAllUser retrieves all User matches certain condition. Returns empty list if  
55 -// no records exist  
56 -func GetAllUser(query map[string]string, fields []string, sortby []string, order []string,  
57 - offset int64, limit int64) (ml []interface{}, err error) {  
58 - o := orm.NewOrm()  
59 - qs := o.QueryTable(new(User))  
60 - // query k=v  
61 - for k, v := range query {  
62 - // rewrite dot-notation to Object__Attribute  
63 - k = strings.Replace(k, ".", "__", -1)  
64 - if strings.Contains(k, "isnull") {  
65 - qs = qs.Filter(k, (v == "true" || v == "1"))  
66 - } else {  
67 - qs = qs.Filter(k, v)  
68 - }  
69 - }  
70 - // order by:  
71 - var sortFields []string  
72 - if len(sortby) != 0 {  
73 - if len(sortby) == len(order) {  
74 - // 1) for each sort field, there is an associated order  
75 - for i, v := range sortby {  
76 - orderby := ""  
77 - if order[i] == "desc" {  
78 - orderby = "-" + v  
79 - } else if order[i] == "asc" {  
80 - orderby = v  
81 - } else {  
82 - return nil, errors.New("Error: Invalid order. Must be either [asc|desc]")  
83 - }  
84 - sortFields = append(sortFields, orderby)  
85 - }  
86 - qs = qs.OrderBy(sortFields...)  
87 - } else if len(sortby) != len(order) && len(order) == 1 {  
88 - // 2) there is exactly one order, all the sorted fields will be sorted by this order  
89 - for _, v := range sortby {  
90 - orderby := ""  
91 - if order[0] == "desc" {  
92 - orderby = "-" + v  
93 - } else if order[0] == "asc" {  
94 - orderby = v  
95 - } else {  
96 - return nil, errors.New("Error: Invalid order. Must be either [asc|desc]")  
97 - }  
98 - sortFields = append(sortFields, orderby)  
99 - }  
100 - } else if len(sortby) != len(order) && len(order) != 1 {  
101 - return nil, errors.New("Error: 'sortby', 'order' sizes mismatch or 'order' size is not 1")  
102 - }  
103 - } else {  
104 - if len(order) != 0 {  
105 - return nil, errors.New("Error: unused 'order' fields")  
106 - }  
107 - }  
108 -  
109 - var l []User  
110 - qs = qs.OrderBy(sortFields...)  
111 - if _, err = qs.Limit(limit, offset).All(&l, fields...); err == nil {  
112 - if len(fields) == 0 {  
113 - for _, v := range l {  
114 - ml = append(ml, v)  
115 - }  
116 - } else {  
117 - // trim unused fields  
118 - for _, v := range l {  
119 - m := make(map[string]interface{})  
120 - val := reflect.ValueOf(v)  
121 - for _, fname := range fields {  
122 - m[fname] = val.FieldByName(fname).Interface()  
123 - }  
124 - ml = append(ml, m)  
125 - }  
126 - }  
127 - return ml, nil  
128 - }  
129 - return nil, err  
130 -}  
131 -  
132 // UpdateUser updates User by Id and returns error if 57 // UpdateUser updates User by Id and returns error if
133 // the record to be updated doesn't exist 58 // the record to be updated doesn't exist
134 func UpdateUserById(m *User) (err error) { 59 func UpdateUserById(m *User) (err error) {
  1 +package models
  2 +
  3 +import (
  4 + "fmt"
  5 + "time"
  6 +
  7 + "github.com/astaxie/beego/orm"
  8 +)
  9 +
  10 +type UserDepartment struct {
  11 + Id int `orm:"column(id);auto" description:"主键"`
  12 + UserId int64 `orm:"column(user_id)" description:"用户id"`
  13 + CompanyId int `orm:"column(company_id)" description:"公司id"`
  14 + DepartmentId int `orm:"column(department_id)" description:"部门id"`
  15 + CreateTime time.Time `orm:"column(create_time);type(timestamp);null" description:"创建时间"`
  16 + EnableStatus int8 `orm:"column(enable_status)" description:"是否有效"`
  17 +}
  18 +
  19 +func (t *UserDepartment) TableName() string {
  20 + return "user_department"
  21 +}
  22 +
  23 +func init() {
  24 + orm.RegisterModel(new(UserDepartment))
  25 +}
  26 +
  27 +// AddUserDepartment insert a new UserDepartment into database and returns
  28 +// last inserted Id on success.
  29 +func AddUserDepartment(m *UserDepartment) (id int64, err error) {
  30 + o := orm.NewOrm()
  31 + id, err = o.Insert(m)
  32 + return
  33 +}
  34 +
  35 +// GetUserDepartmentById retrieves UserDepartment by Id. Returns error if
  36 +// Id doesn't exist
  37 +func GetUserDepartmentById(id int) (v *UserDepartment, err error) {
  38 + o := orm.NewOrm()
  39 + v = &UserDepartment{Id: id}
  40 + if err = o.Read(v); err == nil {
  41 + return v, nil
  42 + }
  43 + return nil, err
  44 +}
  45 +
  46 +// UpdateUserDepartment updates UserDepartment by Id and returns error if
  47 +// the record to be updated doesn't exist
  48 +func UpdateUserDepartmentById(m *UserDepartment) (err error) {
  49 + o := orm.NewOrm()
  50 + v := UserDepartment{Id: m.Id}
  51 + // ascertain id exists in the database
  52 + if err = o.Read(&v); err == nil {
  53 + var num int64
  54 + if num, err = o.Update(m); err == nil {
  55 + fmt.Println("Number of records updated in database:", num)
  56 + }
  57 + }
  58 + return
  59 +}
  60 +
  61 +// DeleteUserDepartment deletes UserDepartment by Id and returns error if
  62 +// the record to be deleted doesn't exist
  63 +func DeleteUserDepartment(id int) (err error) {
  64 + o := orm.NewOrm()
  65 + v := UserDepartment{Id: id}
  66 + // ascertain id exists in the database
  67 + if err = o.Read(&v); err == nil {
  68 + var num int64
  69 + if num, err = o.Delete(&UserDepartment{Id: id}); err == nil {
  70 + fmt.Println("Number of records deleted in database:", num)
  71 + }
  72 + }
  73 + return
  74 +}
  1 +package models
  2 +
  3 +import (
  4 + "fmt"
  5 + "time"
  6 +
  7 + "github.com/astaxie/beego/orm"
  8 +)
  9 +
  10 +type UserPosition struct {
  11 + Id int `orm:"column(id);pk" description:"唯一键值"`
  12 + UserId int64 `orm:"column(user_id)" description:"表user.id 用户编号"`
  13 + PositionId int `orm:"column(position_id)" description:"表position.id 职位编号"`
  14 + CreateAt time.Time `orm:"column(create_at);type(timestamp);null" description:"创建时间"`
  15 + CompanyId int `orm:"column(company_id)" description:"表company.id 公司编号"`
  16 + EnableStatus int8 `orm:"column(enable_status);null" description:"是否有效 1:有效 0:无效"`
  17 +}
  18 +
  19 +func (t *UserPosition) TableName() string {
  20 + return "user_position"
  21 +}
  22 +
  23 +func init() {
  24 + orm.RegisterModel(new(UserPosition))
  25 +}
  26 +
  27 +// AddUserPosition insert a new UserPosition into database and returns
  28 +// last inserted Id on success.
  29 +func AddUserPosition(m *UserPosition) (id int64, err error) {
  30 + o := orm.NewOrm()
  31 + id, err = o.Insert(m)
  32 + return
  33 +}
  34 +
  35 +// GetUserPositionById retrieves UserPosition by Id. Returns error if
  36 +// Id doesn't exist
  37 +func GetUserPositionById(id int) (v *UserPosition, err error) {
  38 + o := orm.NewOrm()
  39 + v = &UserPosition{Id: id}
  40 + if err = o.Read(v); err == nil {
  41 + return v, nil
  42 + }
  43 + return nil, err
  44 +}
  45 +
  46 +// UpdateUserPosition updates UserPosition by Id and returns error if
  47 +// the record to be updated doesn't exist
  48 +func UpdateUserPositionById(m *UserPosition) (err error) {
  49 + o := orm.NewOrm()
  50 + v := UserPosition{Id: m.Id}
  51 + // ascertain id exists in the database
  52 + if err = o.Read(&v); err == nil {
  53 + var num int64
  54 + if num, err = o.Update(m); err == nil {
  55 + fmt.Println("Number of records updated in database:", num)
  56 + }
  57 + }
  58 + return
  59 +}
  60 +
  61 +// DeleteUserPosition deletes UserPosition by Id and returns error if
  62 +// the record to be deleted doesn't exist
  63 +func DeleteUserPosition(id int) (err error) {
  64 + o := orm.NewOrm()
  65 + v := UserPosition{Id: id}
  66 + // ascertain id exists in the database
  67 + if err = o.Read(&v); err == nil {
  68 + var num int64
  69 + if num, err = o.Delete(&UserPosition{Id: id}); err == nil {
  70 + fmt.Println("Number of records deleted in database:", num)
  71 + }
  72 + }
  73 + return
  74 +}
@@ -73,16 +73,18 @@ type RequestUserAdd struct { @@ -73,16 +73,18 @@ type RequestUserAdd struct {
73 Name string `json:"name"` 73 Name string `json:"name"`
74 CompanyId int64 `json:"company_id"` 74 CompanyId int64 `json:"company_id"`
75 Phone string `json:"phone` 75 Phone string `json:"phone`
76 - Departments []int `json:"departments"`  
77 - Positions []int `json:"positions"`  
78 - Roles []int `json:"roles"` 76 + Departments []int64 `json:"departments"`
  77 + Positions []int64 `json:"positions"`
  78 + Roles []int64 `json:"roles"`
79 } 79 }
80 80
81 type RequestUserEdit struct { 81 type RequestUserEdit struct {
82 - Name string `json:"name"`  
83 - CompanyId int64 `json:"company_id"`  
84 - Phone string `json:"phone`  
85 - Departments []int `json:"departments"`  
86 - Positions []int `json:"positions"`  
87 - Roles []int `json:"roles"` 82 + ID int64 `json:"id"`
  83 + RequestUserAdd
  84 + // Name string `json:"name"`
  85 + // CompanyId int64 `json:"company_id"`
  86 + // Phone string `json:"phone`
  87 + // Departments []int `json:"departments"`
  88 + // Positions []int `json:"positions"`
  89 + // Roles []int `json:"roles"`
88 } 90 }
@@ -5,4 +5,7 @@ var errmessge ErrorMap = map[string]string{ @@ -5,4 +5,7 @@ var errmessge ErrorMap = map[string]string{
5 "1": "系统异常", 5 "1": "系统异常",
6 "101": "clientId或clientSecret无效", 6 "101": "clientId或clientSecret无效",
7 "113": "签名验证失败", 7 "113": "签名验证失败",
  8 + //角色相关
  9 + "30001": "请先删除该分组下的其他角色",
  10 + "30002": "请先删除该角色下的人员",
8 } 11 }
1 package protocol 1 package protocol
2 2
3 -//RequestRoleAdd 添加角色信息操作入参 3 +//RequestRoleAdd 添加角色信息
4 type RequestRoleAdd struct { 4 type RequestRoleAdd struct {
5 - CompanyID int `json:"company"`  
6 - Pid int `json:"pid"` 5 + CompanyID int64 `json:"company"`
  6 + Pid int64 `json:"pid"`
7 Name string `json:"name"` 7 Name string `json:"name"`
8 Types int8 `json:"types"` 8 Types int8 `json:"types"`
9 Descript string `json:"descript"` 9 Descript string `json:"descript"`
@@ -11,13 +11,13 @@ type RequestRoleAdd struct { @@ -11,13 +11,13 @@ type RequestRoleAdd struct {
11 11
12 //RequestRoleDelete 删除一个角色数据 12 //RequestRoleDelete 删除一个角色数据
13 type RequestRoleDelete struct { 13 type RequestRoleDelete struct {
14 - CompanyID int `json:"company_id"`  
15 - RoleID int `json:"role_id"` 14 + CompanyID int64 `json:"company_id"`
  15 + RoleID int64 `json:"role_id"`
16 } 16 }
17 17
18 //RequestRoleEdit 编辑角色信息入参 18 //RequestRoleEdit 编辑角色信息入参
19 type RequestRoleEdit struct { 19 type RequestRoleEdit struct {
20 - ID int `json:"id"` 20 + ID int64 `json:"id"`
21 RequestRoleAdd 21 RequestRoleAdd
22 } 22 }
23 23
@@ -29,8 +29,8 @@ type RequestRoleOne struct { @@ -29,8 +29,8 @@ type RequestRoleOne struct {
29 29
30 //ResponseRoleInfo 响应数据 30 //ResponseRoleInfo 响应数据
31 type ResponseRoleInfo struct { 31 type ResponseRoleInfo struct {
32 - ID int `json:"id"`  
33 - Pid int `json:"pid"` 32 + ID int64 `json:"id"`
  33 + Pid int64 `json:"pid"`
34 ParentName string `json:"parent_name"` 34 ParentName string `json:"parent_name"`
35 Name string `json:"name"` 35 Name string `json:"name"`
36 Descript string `json:"descript"` 36 Descript string `json:"descript"`
@@ -22,6 +22,9 @@ func init() { @@ -22,6 +22,9 @@ func init() {
22 beego.NSRouter("/role", &controllers.RbacController{}, "post:RoleAdd"), 22 beego.NSRouter("/role", &controllers.RbacController{}, "post:RoleAdd"),
23 beego.NSRouter("/role", &controllers.RbacController{}, "put:RoleUpdate"), 23 beego.NSRouter("/role", &controllers.RbacController{}, "put:RoleUpdate"),
24 beego.NSRouter("/role", &controllers.RbacController{}, "delete:RoleDelete"), 24 beego.NSRouter("/role", &controllers.RbacController{}, "delete:RoleDelete"),
  25 + beego.NSRouter("/role_group", &controllers.RbacController{}, "post:RoleGroupAdd"),
  26 + beego.NSRouter("/role_group", &controllers.RbacController{}, "put:RoleGroupUpdate"),
  27 + beego.NSRouter("/role_group", &controllers.RbacController{}, "delete:RoleGroupDelete"),
25 ), 28 ),
26 ) 29 )
27 30
  1 +package auth
  2 +
  3 +import (
  4 + "crypto/sha1"
  5 + "encoding/hex"
  6 + "fmt"
  7 + "io"
  8 + "oppmg/common/config"
  9 + "oppmg/common/log"
  10 + "oppmg/models"
  11 + "oppmg/protocol"
  12 + "oppmg/utils"
  13 + "strings"
  14 + "time"
  15 +
  16 + "github.com/astaxie/beego/orm"
  17 +)
  18 +
  19 +//GetAccessToken 获取accessToken
  20 +func GetAccessToken(param protocol.RequestCheckSmsCode) (*protocol.DataUserInfo, error) {
  21 + data := &protocol.DataUserInfo{}
  22 + err := protocol.NewErrWithMessage("00000")
  23 + log.Info("log 打印")
  24 + log.Info("%+v", config.MConfig)
  25 + return data, err
  26 +}
  27 +
  28 +//ValidatePassword ...
  29 +//from:待校验的密码;to:比对用的密文
  30 +func validatePassword(from, to string) bool {
  31 + //密码加密方式sha1
  32 + h := sha1.New()
  33 + io.WriteString(h, from)
  34 + str := hex.EncodeToString(h.Sum(nil))
  35 + if strings.Compare(str, to) == 0 {
  36 + return true
  37 + }
  38 + return false
  39 +}
  40 +
  41 +//LoginAuth 登录认证
  42 +func LoginAuthByPassword(account, password string) error {
  43 + var (
  44 + user *models.User
  45 + uAuth *models.UserAuth
  46 + err error
  47 + )
  48 + user, err = models.GetUserByPhone(account)
  49 + if err != nil {
  50 + log.Error(err.Error())
  51 + return protocol.NewErrWithMessage("1", err)
  52 + }
  53 + if ok := validatePassword(password, user.Passwd); !ok {
  54 + return protocol.NewErrWithMessage("1", err)
  55 + }
  56 +
  57 + uAuth, err = models.ReadUserAuthByDevice(user.Id, models.DEVICE_TYPE_WEB)
  58 + if err != nil && err != orm.ErrNoRows {
  59 + e := fmt.Errorf("ReadUserAuthByDevice(%d,%d) err:%s", user.Id, models.DEVICE_TYPE_WEB, err)
  60 + log.Error(e.Error())
  61 + return protocol.NewErrWithMessage("1", e)
  62 + }
  63 + var (
  64 + authcode string
  65 + authcodeExp time.Time
  66 + )
  67 + authcode = utils.GenerateIDByUUID()
  68 + authcodeExp = time.Now().Add(time.Duration(models.AUTHCODE_TIME) * time.Second)
  69 + if err == orm.ErrNoRows {
  70 + uAuth := &models.UserAuth{
  71 + UserId: user.Id,
  72 + AuthCode: authcode,
  73 + AuthCodeExp: authcodeExp,
  74 + CreateAt: time.Now(),
  75 + }
  76 + _, err = models.AddUserAuth(uAuth)
  77 + if err != nil {
  78 + e := fmt.Errorf("AddUserAuth err:%s", err)
  79 + log.Error(e.Error())
  80 + return protocol.NewErrWithMessage("1", e)
  81 + }
  82 + }
  83 + if err == nil {
  84 + uAuth.AuthCode = authcode
  85 + uAuth.AuthCodeExp = authcodeExp
  86 + uAuth.UpdateAt = time.Now()
  87 + err = models.UpdateUserAuthById(uAuth)
  88 + if err != nil {
  89 + e := fmt.Errorf("UpdateUserAuthById err:%s", err)
  90 + log.Error(e.Error())
  91 + return protocol.NewErrWithMessage("1", e)
  92 + }
  93 + }
  94 +
  95 + return nil
  96 +}
  97 +
  98 +//RefreshAccessToken 刷新token
  99 +func RefreshAccessToken(account string, token string) error {
  100 + return nil
  101 +}
  102 +
  103 +// func buildNewUserAuth(uid int64,) *models.UserAuth {
  104 +// m:=&models.UserAuth{
  105 +// User
  106 +// }
  107 +// return nil
  108 +// }
@@ -304,7 +304,7 @@ func DepartmentDelete(param protocol.RequestDepartmentDelete) error { @@ -304,7 +304,7 @@ func DepartmentDelete(param protocol.RequestDepartmentDelete) error {
304 log.Error(e.Error()) 304 log.Error(e.Error())
305 return protocol.NewErrWithMessage("1", e) 305 return protocol.NewErrWithMessage("1", e)
306 } 306 }
307 - if _, ok := toDelete[subset.Id]; ok { 307 + if _, ok := toDelete[subset.Id]; ok && subset.Id != pos.Id {
308 delete(toDelete, subset.Id) 308 delete(toDelete, subset.Id)
309 } 309 }
310 } 310 }
1 package company 1 package company
2 2
3 -func userAdd() error { 3 +import (
  4 + "fmt"
  5 + "oppmg/common/log"
  6 + "oppmg/models"
  7 + "oppmg/protocol"
  8 +)
  9 +
  10 +func UserAdd(param protocol.RequestUserAdd) error {
  11 + var (
  12 + // user models.User
  13 + // userRoles []models.RoleRelated
  14 + // userPositions []models.UserPosition
  15 + // userDeparts []models.UserPosition
  16 + )
  17 + //校验角色
  18 + validCompanyRole(param.CompanyId, param.Roles)
  19 + //校验部门
  20 + validCompanyDepart(param.CompanyId, param.Departments)
  21 + //校验职位
  22 + validCompanyPositions(param.CompanyId, param.Positions)
  23 + //添加用户
  24 + //添加角色
  25 + //添加部门
  26 + //添加职位
  27 + return nil
  28 +}
  29 +
  30 +func validCompanyRole(companyId int64, roleIds []int64) error {
  31 + for _, v := range roleIds {
  32 + role, err := models.GetRoleById(v)
  33 + if err != nil {
  34 + e := fmt.Errorf("GetRoleById(%d) err:%s", v, err)
  35 + log.Error(e.Error())
  36 + return protocol.NewErrWithMessage("1", err)
  37 + }
  38 + if role.CompanyId != companyId {
  39 + e := fmt.Errorf(" role.CompanyId != param.CompanyId ")
  40 + log.Error(e.Error())
  41 + return protocol.NewErrWithMessage("1", err)
  42 + }
  43 + }
  44 + return nil
  45 +}
  46 +
  47 +func validCompanyDepart(companyId int64, departIds []int64) error {
  48 + for _, v := range departIds {
  49 + depart, err := models.GetDepartmentById(v)
  50 + if err != nil {
  51 + e := fmt.Errorf("GetDepartmentById(%d) err:%s", v, err)
  52 + log.Error(e.Error())
  53 + return protocol.NewErrWithMessage("1", err)
  54 + }
  55 + if depart.CompanyId != companyId {
  56 + e := fmt.Errorf(" depart.CompanyId != param.CompanyId ")
  57 + log.Error(e.Error())
  58 + return protocol.NewErrWithMessage("1", err)
  59 + }
  60 + }
  61 + return nil
  62 +}
  63 +
  64 +func validCompanyPositions(companyId int64, positionIds []int64) error {
  65 + for _, v := range positionIds {
  66 + pos, err := models.GetPositionById(v)
  67 + if err != nil {
  68 + e := fmt.Errorf("GetDepartmentById(%d) err:%s", v, err)
  69 + log.Error(e.Error())
  70 + return protocol.NewErrWithMessage("1", err)
  71 + }
  72 + if pos.CompanyId != companyId {
  73 + e := fmt.Errorf("pos.CompanyId != pos.CompanyId")
  74 + log.Error(e.Error())
  75 + return protocol.NewErrWithMessage("1", err)
  76 + }
  77 + }
  78 + return nil
  79 +}
  80 +
  81 +func UserEdit(param protocol.RequestUserEdit) error {
4 return nil 82 return nil
5 } 83 }
6 84
7 -func userEdit() error { 85 +func UserDelete(param protocol.RequestUserAdd) error {
8 return nil 86 return nil
9 } 87 }
  1 +package filesave
  2 +
  3 +import (
  4 + "fmt"
  5 +
  6 + "github.com/sony/sonyflake"
  7 +)
  8 +
  9 +var sf = sonyflake.NewSonyflake(sonyflake.Settings{})
  10 +
  11 +func GenerateIDBySonyflake() int64 {
  12 + num, _ := sf.NextID()
  13 + return int64(num)
  14 +}
  15 +
  16 +func GenerateNewName() string {
  17 + id := GenerateIDBySonyflake()
  18 + return fmt.Sprint(id)
  19 +}
@@ -40,7 +40,7 @@ func RoleAdd(param protocol.RequestRoleAdd) (*protocol.ResponseRoleInfo, error) @@ -40,7 +40,7 @@ func RoleAdd(param protocol.RequestRoleAdd) (*protocol.ResponseRoleInfo, error)
40 return nil, protocol.NewErrWithMessage("1", err) 40 return nil, protocol.NewErrWithMessage("1", err)
41 } 41 }
42 r := &protocol.ResponseRoleInfo{ 42 r := &protocol.ResponseRoleInfo{
43 - ID: int(roleid), 43 + ID: roleid,
44 Name: role.Name, 44 Name: role.Name,
45 Descript: role.Descript, 45 Descript: role.Descript,
46 Pid: role.Pid, 46 Pid: role.Pid,
@@ -63,12 +63,18 @@ func RoleDelete(param protocol.RequestRoleDelete) error { @@ -63,12 +63,18 @@ func RoleDelete(param protocol.RequestRoleDelete) error {
63 return protocol.NewErrWithMessage("1", e) 63 return protocol.NewErrWithMessage("1", e)
64 } 64 }
65 // 65 //
66 - if role.Types == models.ROLETYPES_GROUP {  
67 - if err := models.DeleteRoleByPid(param.RoleID); err != nil {  
68 - e := fmt.Errorf("DeleteRoleByPid err:%s", err) 66 + if role.Types != models.ROLETYPES_ROLE {
  67 + e := fmt.Errorf("role.types err")
69 log.Error(e.Error()) 68 log.Error(e.Error())
70 - return protocol.NewErrWithMessage("1", e) 69 + return protocol.NewErrWithMessage("1")
  70 + }
  71 + cnt, err := models.CountRoleRelatedByUser(param.RoleID)
  72 + if err != nil {
  73 + log.Error(err.Error())
  74 + return protocol.NewErrWithMessage("1")
71 } 75 }
  76 + if cnt > 0 {
  77 + return protocol.NewErrWithMessage("30001")
72 } 78 }
73 if err := models.DeleteRoleByID(param.RoleID); err != nil { 79 if err := models.DeleteRoleByID(param.RoleID); err != nil {
74 e := fmt.Errorf("DeleteRole err:%s", err) 80 e := fmt.Errorf("DeleteRole err:%s", err)
@@ -119,40 +125,55 @@ func RoleEdit(param protocol.RequestRoleEdit) (*protocol.ResponseRoleInfo, error @@ -119,40 +125,55 @@ func RoleEdit(param protocol.RequestRoleEdit) (*protocol.ResponseRoleInfo, error
119 return r, nil 125 return r, nil
120 } 126 }
121 127
  128 +func RoleGroupDelete(param protocol.RequestRoleDelete) error {
  129 + var (
  130 + roleinfo *models.Role
  131 + err error
  132 + cnt int64
  133 + )
  134 + roleinfo, err = models.GetRoleById(param.RoleID)
  135 + if err != nil {
  136 + e := fmt.Errorf("GetRoleById(%d) err:%s", param.RoleID, err)
  137 + log.Error(e.Error())
  138 + return protocol.NewErrWithMessage("1", e)
  139 + }
  140 + if roleinfo.CompanyId != param.CompanyID {
  141 + e := fmt.Errorf("roleinfo.CompanyId(%d) != param.CompanyID(%d)", roleinfo.CompanyId, param.CompanyID)
  142 + log.Error(e.Error())
  143 + return protocol.NewErrWithMessage("1", e)
  144 + }
  145 + if roleinfo.Types != models.ROLETYPES_GROUP {
  146 + e := fmt.Errorf("it is not group")
  147 + log.Error(e.Error())
  148 + return protocol.NewErrWithMessage("1", e)
  149 + }
  150 + cnt, err = models.CountRoleByPid(roleinfo.Id)
  151 + if err != nil {
  152 + log.Error(err.Error())
  153 + return protocol.NewErrWithMessage("1")
  154 + }
  155 + if cnt > 0 {
  156 + return protocol.NewErrWithMessage("30001")
  157 + }
  158 + err = models.DeleteRoleByID(param.RoleID)
  159 + if err != nil {
  160 + e := fmt.Errorf("DeleteRoleByID(%d) err:%s", param.RoleID, err)
  161 + log.Error(e.Error())
  162 + return protocol.NewErrWithMessage("1", e)
  163 + }
  164 + return nil
  165 +}
  166 +
  167 +//CanSetRole 操作员是否可以操作角色
  168 +func CanSetRole(adminId int64, groupId int64) bool {
  169 + return false
  170 +}
  171 +
122 func RoleGetOne(param protocol.RequestRoleOne) (*protocol.ResponseRoleInfo, error) { 172 func RoleGetOne(param protocol.RequestRoleOne) (*protocol.ResponseRoleInfo, error) {
123 // roledata, err := models.GetRoleById(param.RoleID) 173 // roledata, err := models.GetRoleById(param.RoleID)
124 return nil, nil 174 return nil, nil
125 } 175 }
126 176
127 -// func RoleGetByPage(param protocol.RequestRoleList) (*protocol.ResponseRoleList, error) {  
128 -// var (  
129 -// where string = `a.company_id=? and a.delete_at=0`  
130 -// dataSql string = `SELECT a.id,a.name,a.descript,a.create_at,a.update_at  
131 -// FROM role AS a  
132 -// where ` + where  
133 -// countSql string = `SELECT COUNT(*) FROM role as a where ` + where  
134 -// )  
135 -// var (  
136 -// roleList []protocol.ResponseRoleInfo  
137 -// pageinfo protocol.ResponsePageInfo  
138 -// err error  
139 -// )  
140 -// pagequery := utils.NewQueryDataByPage(countSql, dataSql)  
141 -// pagequery.LimitPage(param.PageIndex, param.PageSize)  
142 -// pagequery.AddParam(param.CompanyId)  
143 -// pageinfo, err = pagequery.Query(&roleList)  
144 -// if err != nil {  
145 -// e := fmt.Errorf("SQL EXECUTE err:%s", err)  
146 -// log.Error(e.Error())  
147 -// return nil, protocol.NewErrWithMessage("1", e)  
148 -// }  
149 -// r := &protocol.ResponseRoleList{  
150 -// ResponsePageInfo: pageinfo,  
151 -// Data: roleList,  
152 -// }  
153 -// return r, nil  
154 -// }  
155 -  
156 func GetRoleGroup() error { 177 func GetRoleGroup() error {
157 return nil 178 return nil
158 } 179 }
@@ -45,7 +45,7 @@ func ExecuteQueryAll(result interface{}, sqlstr string, param ...interface{}) er @@ -45,7 +45,7 @@ func ExecuteQueryAll(result interface{}, sqlstr string, param ...interface{}) er
45 PrintLogSql(sqlstr, param...) 45 PrintLogSql(sqlstr, param...)
46 var err error 46 var err error
47 o := orm.NewOrm() 47 o := orm.NewOrm()
48 - err = ExecuteQueryOneWithOrmer(o, result, sqlstr, param) 48 + err = ExecuteQueryAllWithOrmer(o, result, sqlstr, param)
49 return err 49 return err
50 } 50 }
51 51