作者 唐旭辉

接口完成提交

... ... @@ -6,6 +6,7 @@ import (
"oppmg/models"
"oppmg/protocol"
serverbac "oppmg/services/rbac"
"strconv"
)
type RbacController struct {
... ... @@ -35,8 +36,9 @@ func (c *RbacController) RoleAdd() {
msg = protocol.BadRequestParam("1")
return
}
_, err := serverbac.RoleAdd(param)
msg = protocol.NewReturnResponse(nil, err)
param.Types = models.ROLETYPES_ROLE
r, err := serverbac.RoleAdd(param)
msg = protocol.NewReturnResponse(r, err)
return
}
... ... @@ -58,9 +60,9 @@ func (c *RbacController) RoleUpdate() {
msg = protocol.BadRequestParam("1")
return
}
_, err := serverbac.RoleEdit(param)
msg = protocol.NewReturnResponse(nil, err)
param.Types = models.ROLETYPES_ROLE
r, err := serverbac.RoleEdit(param)
msg = protocol.NewReturnResponse(r, err)
return
}
... ... @@ -168,8 +170,14 @@ func (c *RbacController) RoleList() {
defer func() {
c.ResposeJson(msg)
}()
var err error
msg = protocol.NewReturnResponse(nil, err)
var companyid int64
param := c.Ctx.Input.Param(":companyid")
companyid, _ = strconv.ParseInt(param, 10, 64)
if companyid <= 0 {
msg = protocol.BadRequestParam("1")
return
}
rolelist, err := serverbac.GetRoleList(companyid)
msg = protocol.NewReturnResponse(rolelist, err)
return
}
... ...
... ... @@ -66,7 +66,7 @@ func (t *Department) GetManages() []protocol.DepartmentManager {
log.Error("GetUserNameByIds err :%s", err)
return nil
}
var managesdata []protocol.DepartmentManager
managesdata := []protocol.DepartmentManager{}
for _, v := range users {
m := protocol.DepartmentManager{
Id: v.Id, Name: v.NickName,
... ...
... ... @@ -52,6 +52,7 @@ func AddPosition(m *Position, om ...orm.Ormer) (id int64, err error) {
}
m.CreateAt = time.Now()
m.UpdateAt = time.Now()
m.DeleteAt = time.Unix(0, 0)
id, err = o.Insert(m)
return
}
... ...
... ... @@ -16,6 +16,7 @@ type Role struct {
CompanyId int64 `orm:"column(company_id)"`
Descript string `orm:"column(descript)"`
CreateAt time.Time `orm:"column(create_at);type(timestamp)"`
UpdateAt time.Time `orm:"column(update_at);type(timestamp)"`
DeleteAt time.Time `orm:"column(delete_at);type(timestamp)"`
IsDefault int8 `orm:"column(is_default)"`
}
... ... @@ -30,8 +31,9 @@ func init() {
//角色类型
const (
ROLETYPES_GROUP int8 = 1 //角色组
ROLETYPES_ROLE int8 = 2 //角色
ROLETYPES_ROLE int8 = 1 //角色
ROLETYPES_GROUP int8 = 2 //角色组
)
//默认角色
... ... @@ -58,8 +60,10 @@ func (t *Role) ValidatePid() (*Role, error) {
if err != nil {
return nil, err
}
if roledata.DeleteAt.Unix() != 0 {
return nil, errors.New("roledata.DeleteAt !=0")
if roledata.DeleteAt.Unix() > 0 {
return nil, errors.New("roledata Delete")
}
if roledata.Types != ROLETYPES_GROUP {
return nil, errors.New("roledata.Types != ROLETYPES_GROUP")
... ... @@ -75,6 +79,8 @@ func (t *Role) ValidatePid() (*Role, error) {
func AddRole(m *Role) (id int64, err error) {
o := orm.NewOrm()
m.CreateAt = time.Now()
m.DeleteAt = time.Unix(0, 0)
m.UpdateAt = time.Now()
id, err = o.Insert(m)
return
}
... ... @@ -143,3 +149,36 @@ func CountRoleByPid(pid int64) (int64, error) {
Filter("pid", pid).Count()
return cnt, err
}
func GetRoleGroupsByCompany(companyid int64) ([]Role, error) {
var (
err error
roles []Role
)
o := orm.NewOrm()
_, err = o.QueryTable(&Role{}).
Filter("company_id", companyid).
Filter("types", ROLETYPES_GROUP).
Filter("delete_at", 0).
All(&roles)
if err == orm.ErrNoRows {
return roles, nil
}
return roles, err
}
func GetRoleByPid(pid int64) ([]Role, error) {
var (
err error
roles []Role
)
o := orm.NewOrm()
_, err = o.QueryTable(&Role{}).
Filter("pid", pid).
Filter("delete_at", 0).
All(&roles)
if err == orm.ErrNoRows {
return roles, nil
}
return roles, err
}
... ...
... ... @@ -2,7 +2,6 @@ package models
import (
"fmt"
"strings"
"time"
"github.com/astaxie/beego/orm"
... ... @@ -20,7 +19,7 @@ type User struct {
LastLoginTime time.Time `orm:"column(last_login_time);type(timestamp)" description:"最后一次登录时间"`
CreateAt time.Time `orm:"column(create_at);type(timestamp);auto_now_add" description:"创建时间"`
EnableStatus int8 `orm:"column(enable_status)" description:"是否有效"`
DeleteAt time.Time `orm:"column(delete_time);type(timestamp)"`
DeleteAt time.Time `orm:"column(delete_at);type(timestamp)"`
}
func (t *User) TableName() string {
... ... @@ -103,17 +102,13 @@ func getUserNameByIds(ids []int64) ([]User, error) {
if len(ids) == 0 {
return users, nil
}
const (
dataSql string = `SELECT id,nick_name FROM user WHERE id IN (?) AND delete_at =0`
)
var s []string
for _, v := range ids {
s = append(s, fmt.Sprint(v))
}
o := orm.NewOrm()
_, err = o.Raw(dataSql, strings.Join(s, ",")).QueryRows(&users)
if err != nil {
return nil, err
_, err = o.QueryTable(&User{}).
Filter("id__in", ids).
Filter("delete_at", time.Unix(0, 0)).All(&users)
if err == orm.ErrNoRows {
return users, nil
}
return users, nil
return users, err
}
... ...
... ... @@ -2,7 +2,7 @@ package protocol
//RequestRoleAdd 添加角色信息
type RequestRoleAdd struct {
CompanyID int64 `json:"company"`
CompanyID int64 `json:"company_id"`
Pid int64 `json:"pid"`
Name string `json:"name"`
Types int8 `json:"types"`
... ... @@ -12,7 +12,7 @@ type RequestRoleAdd struct {
//RequestRoleDelete 删除一个角色数据
type RequestRoleDelete struct {
CompanyID int64 `json:"company_id"`
RoleID int64 `json:"role_id"`
RoleID int64 `json:"id"`
}
//RequestRoleEdit 编辑角色信息入参
... ... @@ -29,13 +29,10 @@ type RequestRoleOne struct {
//ResponseRoleInfo 响应数据
type ResponseRoleInfo struct {
ID int64 `json:"id"`
Pid int64 `json:"pid"`
ParentName string `json:"parent_name"`
Name string `json:"name"`
Descript string `json:"descript"`
Types int8 `json:"types"`
CreateTime int64 `json:"create_time`
ID int64 `json:"id"`
Name string `json:"name"`
Descript string `json:"descript"`
Types int8 `json:"types"`
}
//RequestRoleList 分页获取角色列表
... ... @@ -45,9 +42,15 @@ type RequestRoleList struct {
Keyword string `json:"keyword"` // 搜索关键字
}
type ResponseRoleList struct {
ResponsePageInfo
Data []ResponseRoleInfo `json:"data`
type RoleInfo struct {
Id int64 `json:"id"`
Name string `json:"name"`
IsDefault int8 `json:"is_default"`
}
type RoleGroup struct {
RoleInfo
Roles []RoleInfo `json:"roles"`
}
type RequestRolePermission struct {
... ...
... ... @@ -13,12 +13,12 @@ func init() {
beego.NSBefore(middleware.AllowOption),
beego.NSBefore(middleware.LogRequestData, middleware.AuthToken),
beego.NSNamespace("/company",
beego.NSRouter("/:companyid/department", &controllers.CompanyController{}, "get:DepartmentList"),
beego.NSRouter("/department/:id", &controllers.CompanyController{}, "get:DepartmentOne"),
beego.NSRouter("/:companyid([0-9]+)/department", &controllers.CompanyController{}, "get:DepartmentList"),
beego.NSRouter("/department/:id([0-9]+)", &controllers.CompanyController{}, "get:DepartmentOne"),
beego.NSRouter("/department", &controllers.CompanyController{}, "post:DepartmentAdd"),
beego.NSRouter("/department", &controllers.CompanyController{}, "put:DepartmentUpdate"),
beego.NSRouter("/department", &controllers.CompanyController{}, "delete:DepartmentDelete"),
beego.NSRouter("/:companyid/position", &controllers.CompanyController{}, "get:PositionList"),
beego.NSRouter("/:companyid([0-9]+)/position", &controllers.CompanyController{}, "get:PositionList"),
beego.NSRouter("/position", &controllers.CompanyController{}, "post:PositionAdd"),
beego.NSRouter("/position", &controllers.CompanyController{}, "put:PositionEdit"),
beego.NSRouter("/position", &controllers.CompanyController{}, "delete:PositionDelete"),
... ... @@ -30,7 +30,7 @@ func init() {
beego.NSRouter("/role_group", &controllers.RbacController{}, "post:RoleGroupAdd"),
beego.NSRouter("/role_group", &controllers.RbacController{}, "put:RoleGroupUpdate"),
beego.NSRouter("/role_group", &controllers.RbacController{}, "delete:RoleGroupDelete"),
beego.NSRouter("/:companyid/role", &controllers.RbacController{}, "get:RoleList"),
beego.NSRouter("/:companyid([0-9]+)/role", &controllers.RbacController{}, "get:RoleList"),
),
)
... ...
... ... @@ -5,6 +5,7 @@ import (
"oppmg/common/log"
"oppmg/models"
"oppmg/protocol"
"oppmg/utils"
"time"
)
... ... @@ -40,12 +41,10 @@ func RoleAdd(param protocol.RequestRoleAdd) (*protocol.ResponseRoleInfo, error)
return nil, protocol.NewErrWithMessage("1", err)
}
r := &protocol.ResponseRoleInfo{
ID: roleid,
Name: role.Name,
Descript: role.Descript,
Pid: role.Pid,
Types: role.Types,
CreateTime: role.CreateAt.Unix(), //Format("2006-01-02 15:04:05")
ID: roleid,
Name: role.Name,
Descript: role.Descript,
Types: role.Types,
}
return r, nil
}
... ... @@ -53,7 +52,7 @@ func RoleAdd(param protocol.RequestRoleAdd) (*protocol.ResponseRoleInfo, error)
func RoleDelete(param protocol.RequestRoleDelete) error {
role, err := models.GetRoleById(param.RoleID)
if err != nil {
e := fmt.Errorf("GetRoleById err:%s", err)
e := fmt.Errorf("GetRoleById(%d) err:%s", param.RoleID, err)
log.Error(e.Error())
return protocol.NewErrWithMessage("1", e)
}
... ... @@ -86,7 +85,6 @@ func RoleDelete(param protocol.RequestRoleDelete) error {
func RoleEdit(param protocol.RequestRoleEdit) (*protocol.ResponseRoleInfo, error) {
var (
//parentRole *models.Role
role *models.Role
err error
)
... ... @@ -115,12 +113,10 @@ func RoleEdit(param protocol.RequestRoleEdit) (*protocol.ResponseRoleInfo, error
return nil, protocol.NewErrWithMessage("1", e)
}
r := &protocol.ResponseRoleInfo{
ID: role.Id,
Name: role.Name,
Descript: role.Descript,
Pid: role.Pid,
Types: role.Types,
CreateTime: role.CreateAt.Unix(),
ID: role.Id,
Name: role.Name,
Descript: role.Descript,
Types: role.Types,
}
return r, nil
}
... ... @@ -169,6 +165,57 @@ func CanSetRole(adminId int64, groupId int64) bool {
return false
}
func GetRoleList(companyId int64) ([]protocol.RoleGroup, error) {
var (
roleGroups []protocol.RoleGroup
err error
groups []protocol.RoleInfo
)
groups, err = getRoleGroupByCompany(companyId)
if err != nil {
log.Error("getRoleGroupByCompany err:%s", err)
return roleGroups, protocol.NewErrWithMessage("1")
}
for k, v := range groups {
var roles []protocol.RoleInfo
roles, err = getRoleInfoByPid(companyId, v.Id)
if err != nil {
log.Error("getRoleInfoByPid err:%s", err)
return roleGroups, protocol.NewErrWithMessage("1")
}
rolegroup := protocol.RoleGroup{
RoleInfo: groups[k],
Roles: roles,
}
roleGroups = append(roleGroups, rolegroup)
}
return roleGroups, nil
}
func getRoleInfoByPid(companyid int64, pid int64) ([]protocol.RoleInfo, error) {
const (
dataSql string = `SELECT id,name,is_default FROM role WHERE company_id=? AND pid=? and delete_at=0 `
)
var (
err error
roles []protocol.RoleInfo
)
err = utils.ExecuteQueryAll(&roles, dataSql, companyid, pid)
return roles, err
}
func getRoleGroupByCompany(companyid int64) ([]protocol.RoleInfo, error) {
const (
datasql string = `SELECT id,name,is_default FROM role WHERE company_id=? AND types=? AND delete_at=0 `
)
var (
err error
roles []protocol.RoleInfo
)
err = utils.ExecuteQueryAll(&roles, datasql, companyid, models.ROLETYPES_GROUP)
return roles, err
}
func RoleGetOne(param protocol.RequestRoleOne) (*protocol.ResponseRoleInfo, error) {
// roledata, err := models.GetRoleById(param.RoleID)
return nil, nil
... ...