作者 唐旭辉

认证bug修复,部门管理各种数据校验

... ... @@ -28,8 +28,8 @@ log_level = "${LOG_LEVEL||error}"
##统一用户中心相关配置
ucenter_check_alt = "rsF0pL!6DwjBO735"
ucenter_base_url = "http://suplus-ucenter-dev.fjmaimaimai.com"
ucenter_app_key = "39aefef9e22744a3b2d2d3791824ae7b"
ucenter_secret = "39aefef9e22744a3b2d2d3791824ae7b"
ucenter_app_key = "0692f2b7e8d6dbd8526579864b87e3bfbc5d5c31"
ucenter_secret = "0692f2b7e8d6dbd8526579864b87e3bfbc5d5c31"
# 审批流程修改 消息发布
message_publish = "audit_change"
#---自定义配置 结束----
\ No newline at end of file
... ...
... ... @@ -28,8 +28,8 @@ log_level = "${LOG_LEVEL||error}"
##统一用户中心相关配置
ucenter_check_alt = "rsF0pL!6DwjBO735"
ucenter_base_url = "http://suplus-ucenter-dev.fjmaimaimai.com"
ucenter_app_key = "39aefef9e22744a3b2d2d3791824ae7b"
ucenter_secret = "39aefef9e22744a3b2d2d3791824ae7b"
ucenter_app_key = "0692f2b7e8d6dbd8526579864b87e3bfbc5d5c31"
ucenter_secret = "0692f2b7e8d6dbd8526579864b87e3bfbc5d5c31"
# 审批流程修改 消息发布
message_publish = "audit_change"
#---自定义配置 结束----
\ No newline at end of file
... ...
... ... @@ -78,6 +78,19 @@ func (c *CompanyController) DepartmentAdd() {
msg = protocol.BadRequestParam("1")
return
}
s := []rune(param.Name)
if len(s) == 0 {
msg = protocol.BadRequestParam("10044")
return
}
if len(s) > 20 {
msg = protocol.BadRequestParam("10043")
return
}
if param.ParentID <= 0 {
msg = protocol.BadRequestParam("10042")
return
}
param.CompanyID = c.GetCompanyId()
if param.CompanyID <= 0 {
log.Error("param.CompanyID <= 0")
... ... @@ -103,6 +116,15 @@ func (c *CompanyController) DepartmentUpdate() {
msg = protocol.BadRequestParam("1")
return
}
s := []rune(param.Name)
if len(s) == 0 {
msg = protocol.BadRequestParam("10044")
return
}
if len(s) > 20 {
msg = protocol.BadRequestParam("10043")
return
}
param.CompanyID = c.GetCompanyId()
if param.ID <= 0 {
log.Error(" param.ID <= 0 ")
... ...
... ... @@ -40,9 +40,7 @@ var AuthToken = func(ctx *context.Context) {
accesstoken := ctx.Input.Header(protocol.HeaderAccessToken)
refreshToken := ctx.Input.Header(protocol.HeaderRefreshToken)
mtoken, err = serveauth.ValidJWTToken(accesstoken)
if beego.BConfig.RunMode != "prod" {
return
}
if err == nil {
storetoken, err = redisdata.GetLoginToken(mtoken.UID)
if err != nil {
... ...
... ... @@ -21,6 +21,7 @@ type Department struct {
DeleteAt time.Time `orm:"column(delete_at);type(timestamp)" description:"删除时间"`
UpdateAt time.Time `orm:"column(update_at);type(timestamp)" description:"更新时间"`
Manages string `orm:"column(managers)" description:"部门负责人id列表 json 数组 []"` //存user_company_id
IsTop int8 `orm:"column(is_top)" `
}
func (t *Department) TableName() string {
... ... @@ -105,6 +106,13 @@ func (t *Department) GetMembers() []protocol.DepartmentMember {
return managesdata
}
func (t *Department) IsTopDepartment() bool {
if t.IsTop == 1 {
return true
}
return false
}
// AddDepartment insert a new Department into database and returns
// last inserted Id on success.
func AddDepartment(m *Department, om ...orm.Ormer) (id int64, err error) {
... ...
... ... @@ -119,10 +119,14 @@ func EnableUserCompany(userid int64, companyid int64) error {
}
func GetUserCompanyReal(ids []int64) ([]UserCompany, error) {
var (
err error
data []UserCompany
)
if len(ids) == 0 {
return data, nil
}
o := orm.NewOrm()
_, err = o.QueryTable(&UserCompany{}).
Filter("id__in", ids).
... ...
... ... @@ -63,42 +63,43 @@ func AddUserDepartment(m *UserDepartment) (id int64, err error) {
// GetUserDepartmentById retrieves UserDepartment by Id. Returns error if
// Id doesn't exist
func GetUserDepartmentById(id int64) (v *UserDepartment, err error) {
o := orm.NewOrm()
v = &UserDepartment{Id: id}
if err = o.Read(v); err == nil {
return v, nil
}
return nil, err
}
// func GetUserDepartmentById(id int64) (v *UserDepartment, err error) {
// o := orm.NewOrm()
// v = &UserDepartment{Id: id}
// if err = o.Read(v); err == nil {
// return v, nil
// }
// return nil, err
// }
// UpdateUserDepartment updates UserDepartment by Id and returns error if
// the record to be updated doesn't exist
func UpdateUserDepartmentById(m *UserDepartment) (err error) {
o := orm.NewOrm()
v := UserDepartment{Id: m.Id}
// ascertain id exists in the database
if err = o.Read(&v); err == nil {
var num int64
if num, err = o.Update(m); err == nil {
fmt.Println("Number of records updated in database:", num)
}
}
return
}
func GetUserDepartment(userid, companyid int64) ([]*UserDepartment, error) {
o := orm.NewOrm()
var (
err error
result []*UserDepartment
)
_, err = o.QueryTable(&UserDepartment{}).
Filter("user_id", userid).
Filter("company_id", companyid).
Filter("enable_status", 1).
All(&result)
return result, err
}
// func UpdateUserDepartmentById(m *UserDepartment) (err error) {
// o := orm.NewOrm()
// v := UserDepartment{Id: m.Id}
// // ascertain id exists in the database
// if err = o.Read(&v); err == nil {
// var num int64
// if num, err = o.Update(m); err == nil {
// fmt.Println("Number of records updated in database:", num)
// }
// }
// return
// }
// func GetUserDepartment(userid, companyid int64) ([]*UserDepartment, error) {
// o := orm.NewOrm()
// var (
// err error
// result []*UserDepartment
// )
// _, err = o.QueryTable(&UserDepartment{}).
// Filter("user_id", userid).
// Filter("company_id", companyid).
// Filter("enable_status", 1).
// All(&result)
// return result, err
// }
func CountUserDepartByDepart(departid int64) (int64, error) {
var (
... ... @@ -125,3 +126,16 @@ func GetUserDepartmentIds(companyId, dId int) (v []int64, err error) {
}
return
}
func ExistUserDepart(departid int64, usercompanyid int64) bool {
var (
ok bool
)
o := orm.NewOrm()
ok = o.QueryTable(&UserDepartment{}).
Filter("department_id", departid).
Filter("user_company_id", usercompanyid).
Filter("delete_at", 0).
Exist()
return ok
}
... ...
... ... @@ -27,6 +27,11 @@ var errmessge ErrorMap = map[string]string{
"10037": "用户的部门必填",
"10038": "用户的角色必填",
"10039": "用户已存在",
//部门相关
"10041": "无效的主管设置",
"10042": "无效的上级部门",
"10043": "部门名称限制不超过20个字符",
"10044": "部门名称必填",
}
//错误码转换 ,兼容需要
... ...
... ... @@ -12,7 +12,7 @@ func init() {
nsV1 := beego.NewNamespace("v1",
beego.NSBefore(middleware.AllowOption),
beego.NSBefore(middleware.LogRequestData),
//beego.NSBefore(middleware.AuthToken),
beego.NSBefore(middleware.AuthToken),
beego.NSNamespace("/department",
beego.NSRouter("/list", &controllers.CompanyController{}, "post:DepartmentList"),
beego.NSRouter("/add", &controllers.CompanyController{}, "post:DepartmentAdd"),
... ...
... ... @@ -224,12 +224,12 @@ func RefreshLoginToken(refreshtoken string) (protocol.LoginAuthToken, error) {
func LoginAuthByUCenter(account, password string) (protocol.LoginAuthToken, error) {
var (
err error
logintoken protocol.LoginAuthToken
uclientReturn ucenter.ResponseLogin
companys []companybase
companyid int64
userdata *models.User
err error
logintoken protocol.LoginAuthToken
companys []companybase
companyid int64
userdata *models.User
)
userdata, err = models.GetUserByPhone(account)
if err != nil {
... ... @@ -253,6 +253,7 @@ func LoginAuthByUCenter(account, password string) (protocol.LoginAuthToken, erro
return logintoken, protocol.NewErrWithMessage("10021")
}
companyid = companys[0].Id
var uclientReturn ucenter.ResponseLogin
param := ucenter.RequesLogin{
Phone: account,
Password: password,
... ... @@ -283,27 +284,28 @@ type companybase struct {
}
//getUserCompanyReal 获取用户有效的公司id
func getUserCompanyReal(userid int64) (companyids []companybase, err error) {
func getUserCompanyReal(userid int64) ([]companybase, error) {
const (
datasql0 string = `SELECT b.company_id FROM user_company AS b
WHERE b.delete_at=0 AND b.enable = 1 AND b.user_id=?`
datasql2 string = `SELECT b.id,a.name,a.logo FROM company AS b
datasql2 string = `SELECT b.id,b.name,b.logo FROM company AS b
WHERE b.delete_at=0 AND b.enable = 1 AND b.id in (%s) `
)
var (
companys []companybase
ids []string
err error
)
err = utils.ExecuteQueryAll(&ids, datasql0, userid)
if err != nil {
log.Error("EXECUTE SQL err:%s", err)
return nil, err
}
if len(companys) == 0 {
if len(ids) == 0 {
return nil, nil
}
datasql3 := fmt.Sprintf(datasql2, strings.Join(ids, ","))
err = utils.ExecuteQueryAll(&companyids, datasql3)
err = utils.ExecuteQueryAll(&companys, datasql3)
if err != nil {
log.Error("EXECUTE SQL err:%s", err)
return nil, err
... ...
... ... @@ -18,6 +18,10 @@ func DepartmentAdd(param protocol.RequestDepartmentAdd) (protocol.ResponseDepart
returndata protocol.ResponseDepartmenrAdd
err error
)
if param.ParentID == 0 {
//部门必定有上级部门 ,至少是公司一级
return returndata, protocol.NewErrWithMessage("10042")
}
if param.ParentID > 0 {
parentDepart, err = models.GetDepartmentById(param.ParentID)
if err != nil {
... ... @@ -100,22 +104,46 @@ func DepartmentEdit(param protocol.RequestDepartmentEdit) error {
log.Error(e.Error())
return protocol.NewErrWithMessage("1", e)
}
//校验数据
if ok := departUpdate.IsTopDepartment(); ok {
//公司一级不能修改上级,不能修改名称
param.Name = departUpdate.Name
param.ParentID = departUpdate.ParentId
} else {
if param.ParentID <= 0 {
return protocol.NewErrWithMessage("10042")
}
}
//确认部门主管变更情况
var (
oldmanage []int64
newManage []int64
)
oldmanage = departUpdate.GetManagesIds()
diffmanage := utils.ArrayInt64Diff(param.Managers, oldmanage)
for i := range diffmanage {
_, err = models.GetUserCompanyBy(diffmanage[i], param.CompanyID)
if err != nil {
e := fmt.Errorf("GetUserCompanyBy(userid,companyid)[%d,%d] err:%s", diffmanage[i], param.CompanyID, err)
//oldmanage = departUpdate.GetManagesIds()
//diffmanage := utils.ArrayInt64Diff(param.Managers, oldmanage)
uc, err := models.GetUserCompanyReal(param.Managers)
if err != nil {
e := fmt.Errorf("GetUserCompanyReal err:%s", err)
log.Error(e.Error())
return protocol.NewErrWithMessage("10041")
}
for i := range uc {
if uc[i].CompanyId != param.CompanyID {
e := fmt.Errorf("managers err")
log.Error(e.Error())
return protocol.NewErrWithMessage("1", e)
return protocol.NewErrWithMessage("10041")
}
//检查设置的主管是否是部门下的人员
ok := models.ExistUserDepart(param.ID, uc[i].Id)
if !ok {
log.Error("false=aExistUserDepart(departid,usercompanyid) [%d,%d]", param.ID, uc[i].Id)
return protocol.NewErrWithMessage("10041")
}
newManage = append(newManage, uc[i].Id)
}
departUpdate.SetManages(param.Managers)
departUpdate.SetManages(newManage)
departUpdate.Name = param.Name
departUpdate.UpdateAt = time.Now()
err = models.UpdateDepartmentById(departUpdate, []string{"Manages", "Name", "UpdateAt"})
if err != nil {
... ... @@ -127,7 +155,9 @@ func DepartmentEdit(param protocol.RequestDepartmentEdit) error {
var (
newparent *models.Department
)
if departUpdate.ParentId != param.ParentID {
//检查上级部门是否合法
newparent, err = models.GetDepartmentById(param.ParentID)
if err != nil {
e := fmt.Errorf("GetDepartmentById(%d) err:%s", param.ParentID, err)
... ... @@ -195,7 +225,7 @@ func departmentRelationUpdate(departUpdate *models.Department, newparent *models
o.Rollback()
e := fmt.Errorf("departSubset[i].Id == newparent.Id")
log.Error(e.Error())
return protocol.NewErrWithMessage("1", e)
return protocol.NewErrWithMessage("10042")
}
//重建关系树
s := strings.TrimPrefix(departSubset[i].Relation, oldRelation)
... ... @@ -234,6 +264,10 @@ func DepartmentDelete(param protocol.RequestDepartmentDelete) error {
log.Error(e.Error())
return protocol.NewErrWithMessage("1", e)
}
//一级部门不能删除
if ok := p.IsTopDepartment(); ok {
continue
}
if p.CompanyId != param.CompanyID {
e := fmt.Errorf("CompanyID err")
log.Error(e.Error())
... ...