作者 唐旭辉

“用户角色权限设置”进行中

... ... @@ -5,7 +5,6 @@ import (
"oppmg/common/log"
"oppmg/protocol"
servecompany "oppmg/services/company"
"oppmg/storage/redisdata"
"oppmg/utils"
"strings"
)
... ... @@ -302,7 +301,7 @@ func (c *CompanyController) UserDelete() {
c.ResposeJson(msg)
}()
type Parameter struct {
Userid int64 `json:"user_id"`
UserCompanyId []int64 `json:"ids"`
}
var param Parameter
if err := json.Unmarshal(c.Ctx.Input.RequestBody, &param); err != nil {
... ... @@ -311,20 +310,15 @@ func (c *CompanyController) UserDelete() {
return
}
companyId := c.GetCompanyId()
err := servecompany.UserDelete(param.Userid, companyId)
if err == nil {
e := redisdata.DeleteLoginToken(param.Userid)
if e != nil {
log.Error(e.Error())
}
}
err := servecompany.UserDelete(param.UserCompanyId, companyId)
msg = protocol.NewReturnResponse(nil, err)
return
}
//UserEdit 禁用、启用用户 TODO
//@Router /user/forbid [post]
func (c *CompanyController) UserForbid() {
//@Router /user/allow_forbid [post]
func (c *CompanyController) UserAllowForbid() {
var msg *protocol.ResponseMessage
defer func() {
c.ResposeJson(msg)
... ... @@ -333,14 +327,27 @@ func (c *CompanyController) UserForbid() {
UserCompanyIds []int64 `json:"ids"`
Status int `json:"status"` // [1:执行禁用操作][2:执行启用操作]
}
var param protocol.RequestUserEdit
var param Parameter
if err := json.Unmarshal(c.Ctx.Input.RequestBody, &param); err != nil {
log.Error("json 解析失败 err:%s", err)
msg = protocol.BadRequestParam("1")
return
}
var (
err error
)
companyid := c.GetCompanyId()
switch param.Status {
case 1:
err = servecompany.UserForbid(param.UserCompanyIds, companyid)
case 2:
err = servecompany.UserAllow(param.UserCompanyIds, companyid)
default:
msg = protocol.BadRequestParam("1")
return
}
msg = protocol.NewReturnResponse(nil, nil)
msg = protocol.NewReturnResponse(nil, err)
return
}
... ... @@ -406,3 +413,45 @@ func (c *CompanyController) DepartmentUser() {
msg = protocol.NewReturnResponse(resultdata, err)
return
}
//CurrentCompanyEdit 编辑当前公司
func (c *CompanyController) CurrentCompanyEdit() {
var msg *protocol.ResponseMessage
defer func() {
c.ResposeJson(msg)
}()
type Parameter struct {
Logo string `json:"string"`
}
var param Parameter
if err := json.Unmarshal(c.Ctx.Input.RequestBody, &param); err != nil {
log.Error("json 解析失败 err:%s", err)
msg = protocol.BadRequestParam("1")
return
}
companyid := c.GetCompanyId()
err := servecompany.CompanyInfoEdit(companyid, param.Logo)
msg = protocol.NewReturnResponse(nil, err)
return
}
//CurrentCompanyInfo 获取当前公司数据
func (c *CompanyController) CurrentCompanyInfo() {
var msg *protocol.ResponseMessage
defer func() {
c.ResposeJson(msg)
}()
// type Parameter struct {
// Logo string `json:"string"`
// }
// var param Parameter
// if err := json.Unmarshal(c.Ctx.Input.RequestBody, &param); err != nil {
// log.Error("json 解析失败 err:%s", err)
// msg = protocol.BadRequestParam("1")
// return
// }
companyid := c.GetCompanyId()
companybase, err := servecompany.CompanyBaseInfo(companyid)
msg = protocol.NewReturnResponse(companybase, err)
return
}
... ...
... ... @@ -188,3 +188,41 @@ func (c *RbacController) RoleList() {
msg = protocol.NewReturnResponse(rolelist, err)
return
}
//PermissionList
//@router /menu/list [post]
func (c *RbacController) MenuList() {
var msg *protocol.ResponseMessage
defer func() {
c.ResposeJson(msg)
}()
list, err := serverbac.GetMenuAll()
msg = protocol.NewReturnResponse(list, err)
return
}
//GetRoleHasMenu 获取角色下的菜单
//@router /role/menu
func (c *RbacController) RoleHasMenu() {
var msg *protocol.ResponseMessage
defer func() {
c.ResposeJson(msg)
}()
type Parameter struct {
RoleId int64 `json:"role_id"`
}
var param Parameter
if err := json.Unmarshal(c.Ctx.Input.RequestBody, &param); err != nil {
log.Error("json 解析失败 err:%s", err)
msg = protocol.BadRequestParam("1")
return
}
companyid := c.GetCompanyId()
list, err := serverbac.GetRoleHasMenu(param.RoleId, companyid)
msg = protocol.NewReturnResponse(list, err)
return
}
func (c *RbacController) RoleMenuEdit() {
return
}
... ...
... ... @@ -82,13 +82,14 @@ func GetCompanyById(id int64) (v *Company, err error) {
// UpdateCompany updates Company by Id and returns error if
// the record to be updated doesn't exist
func UpdateCompanyById(m *Company) (err error) {
func UpdateCompanyById(m *Company, col []string) (err error) {
o := orm.NewOrm()
v := Company{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 {
m.UpdateAt = time.Now()
if num, err = o.Update(m, col...); err == nil {
fmt.Println("Number of records updated in database:", num)
}
}
... ...
package models
import (
"time"
"github.com/astaxie/beego/orm"
)
type Menu struct {
Id int `orm:"column(id);auto" description:"主键"`
Name string `orm:"column(name);size(10)" description:"菜单名称"`
Icon string `orm:"column(icon);size(128)" description:"菜单图标地址"`
ParentId int `orm:"column(parent_id)" description:"上一级菜单 id"`
Type int `orm:"column(type)" description:"类型:1为 pc 2为app"`
Url string `orm:"column(url);size(128)" description:"菜单地址"`
SeniorStatus int `orm:"column(senior_status)" description:"类型:是否有高级设置 1是 0 否"`
Code string `orm:"column(code);size(128)" description:"菜单编码"`
Sort int `orm:"column(sort)" description:"同级菜单序号 "`
CreateTime time.Time `orm:"column(create_time);type(timestamp);null" description:"创建时间"`
Enabled int8 `orm:"column(enabled)" description:"是否有效"`
}
func (t *Menu) TableName() string {
return "menu"
}
func init() {
orm.RegisterModel(new(Menu))
}
// GetMenuById retrieves Menu by Id. Returns error if
// Id doesn't exist
func GetMenuById(id int) (v *Menu, err error) {
o := orm.NewOrm()
v = &Menu{Id: id}
if err = o.Read(v); err == nil {
return v, nil
}
return nil, err
}
... ...
package models
import (
"github.com/astaxie/beego/orm"
)
type RoleMenu struct {
Id int64 `orm:"column(id);auto" description:"主键"`
RoleId int64 `orm:"column(role_id);null" description:"role.id"`
MenuId int64 `orm:"column(menu_id);null" description:"menu.id"`
Opption string `orm:"column(opption);size(500);null" description:"配置"`
}
func (t *RoleMenu) TableName() string {
return "role_menu"
}
func init() {
orm.RegisterModel(new(RoleMenu))
}
// AddRoleMenu insert a new RoleMenu into database and returns
// last inserted Id on success.
func AddRoleMenu(m *RoleMenu) (id int64, err error) {
o := orm.NewOrm()
id, err = o.Insert(m)
return
}
func GetRoleMenuByRole(roleid int64) ([]RoleMenu, error) {
var (
list []RoleMenu
err error
)
o := orm.NewOrm()
_, err = o.QueryTable(&RoleMenu{}).
Filter("role_id", roleid).
All(&list)
if err == orm.ErrNoRows {
return list, nil
}
return list, err
}
... ...
... ... @@ -114,3 +114,9 @@ type UserListItem struct {
Departments string `json:"departments" orm:"-"`
Enable int8 `json:"enable" orm:"column(enable)"`
}
type ResponseCompanyBase struct {
CompanyId int64 `json:"id"`
Name string `json:"name"`
Logo string `json:"logo"`
}
... ...
... ... @@ -3,7 +3,7 @@ package protocol
var errmessge ErrorMap = map[string]string{
//操作
"0": "成功",
"1": "无效请求",
"1": "网络连接无响应",
//角色相关
"10001": "请先删除该分组下的其他角色",
"10002": "请先删除该角色下的人员",
... ...
... ... @@ -57,3 +57,24 @@ type RequestRolePermission struct {
RoleID int `json:"role_id"`
PermissionID []int `json:"permission_id"`
}
type PermissionItem struct {
Id int64 `json:"id"`
Name string `json:"name"`
Icon string `json:"icon"`
ParentId int64 `json:"parent_id"`
SeniorStatus int8 `json:"senior_status"`
Sort int `json:"sort"`
}
//TODO PermissionItem 实现排序接口
//
type ResponseRoleMenus struct {
RoleId int64 `json:"role_id"`
RoleName string `json:"role_name"`
MenuId []int64 `json:"menu_ids"`
}
type ResponseRolePermission struct {
}
... ...
package routers
// type PermissionBase struct {
// Check int8 `json:"check"`
// }
//职位管理相关
type PermissionWithPosition struct {
Check int8 `json:"check"`
}
func (c PermissionWithPosition) Code() string {
return "OPPMG_POSITION"
}
//部门管理相关
type PermissionWithDepart struct {
Check int8 `json:"check"`
}
... ...
... ... @@ -9,7 +9,7 @@ import (
)
func init() {
nsV1 := beego.NewNamespace("v1",
nsV1 := beego.NewNamespace("/v1",
beego.NSBefore(middleware.AllowOption),
beego.NSBefore(middleware.LogRequestData),
//beego.NSBefore(middleware.AuthToken),
... ... @@ -34,12 +34,15 @@ func init() {
beego.NSRouter("/role_group/edit", &controllers.RbacController{}, "post:RoleGroupUpdate"),
beego.NSRouter("/role_group/delete", &controllers.RbacController{}, "post:RoleGroupDelete"),
beego.NSRouter("/role/list", &controllers.RbacController{}, "post:RoleList"),
beego.NSRouter("/menu/list", &controllers.RbacController{}, "post:MenuList"),
beego.NSRouter("/role/menu", &controllers.RbacController{}, "post:RoleHasMenu"),
),
beego.NSNamespace("/user",
beego.NSRouter("/list", &controllers.CompanyController{}, "post:UserList"),
beego.NSRouter("/add", &controllers.CompanyController{}, "post:UserAdd"),
beego.NSRouter("/edit", &controllers.CompanyController{}, "post:UserEdit"),
beego.NSRouter("/delete", &controllers.CompanyController{}, "post:UserDelete"),
beego.NSRouter("/allow_forbid", &controllers.CompanyController{}, "post:UserAllowForbid"),
),
beego.NSNamespace("/auth",
beego.NSRouter("/change_company", &controllers.AuthController{}, "post:ChangeCompany"),
... ...
... ... @@ -406,3 +406,34 @@ func initUserRole(newUserCompany *models.UserCompany, newRole *models.Role, o or
return newUserRole, nil
}
func CompanyInfoEdit(companyid int64, logo string) error {
data := models.Company{
Id: companyid,
Logo: logo,
}
err := models.UpdateCompanyById(&data, []string{"Logo"})
if err != nil {
log.Info("更新公司数据失败:%s", err)
return protocol.NewErrWithMessage("1")
}
return nil
}
func CompanyBaseInfo(companyid int64) (*protocol.ResponseCompanyBase, error) {
var (
err error
mcompany *models.Company
)
mcompany, err = models.GetCompanyById(companyid)
if err != nil {
log.Error("获取公司基础数据失败:%s", err)
return nil, protocol.NewErrWithMessage("1")
}
companyinfo := &protocol.ResponseCompanyBase{
CompanyId: mcompany.Id,
Name: mcompany.Name,
Logo: mcompany.Logo,
}
return companyinfo, nil
}
... ...
... ... @@ -5,6 +5,7 @@ import (
"oppmg/common/log"
"oppmg/models"
"oppmg/protocol"
"oppmg/storage/redisdata"
"oppmg/utils"
"strings"
"time"
... ... @@ -375,17 +376,31 @@ func UserEdit(param protocol.RequestUserEdit) error {
return nil
}
func UserDelete(userid, companyid int64) error {
ok := models.ExistUserCompany(userid, companyid)
if !ok {
e := fmt.Errorf("ExistUserCompany(userid, companyid) [%d,%d] ==false ", userid, companyid)
log.Error(e.Error())
return protocol.NewErrWithMessage("1")
func UserDelete(userCompanyids []int64, companyid int64) error {
if len(userCompanyids) == 0 {
return nil
}
var (
updateIds []models.UserCompany
err error
)
o := orm.NewOrm()
_, err := o.QueryTable(&models.UserCompany{}).
Filter("user_id", userid).
Filter("company_id").
_, err = o.QueryTable(&models.UserCompany{}).
Filter("id__in", userCompanyids).
Filter("company_id", companyid).
All(&updateIds, "Id", "UserId")
if err != nil {
log.Error("获取用户数据失败:%s", err)
return protocol.NewErrWithMessage("1")
}
var (
ids []int64
)
for _, v := range updateIds {
ids = append(ids, v.Id)
}
_, err = o.QueryTable(&models.UserCompany{}).
Filter("id__in", ids).
Update(orm.Params{
"delete_at": time.Now().String(),
})
... ... @@ -394,6 +409,13 @@ func UserDelete(userid, companyid int64) error {
log.Error(e.Error())
return protocol.NewErrWithMessage("1")
}
for _, v := range updateIds {
e := redisdata.DeleteLoginToken(v.UserId)
if e != nil {
log.Error(e.Error())
}
}
return nil
}
... ... @@ -510,22 +532,64 @@ func GetUserRole(usercompanyid int64) []protocol.RoleBase {
return data
}
//启用、禁用用户
func UserForbid(userCompanyIds []int64, companyid int64, enable int8) error {
if len(userCompanyIds) == 0 {
//禁用用户
func UserForbid(userCompanyids []int64, companyid int64) error {
if len(userCompanyids) == 0 {
return nil
}
if !(enable == models.USERCOMPANY_ENABLE_NO || enable == models.USERCOMPANY_ENABLE_YES) {
log.Error("enable err")
var (
updateIds []models.UserCompany
err error
)
o := orm.NewOrm()
_, err = o.QueryTable(&models.UserCompany{}).
Filter("id__in", userCompanyids).
Filter("company_id", companyid).
All(&updateIds, "Id", "UserId")
if err != nil {
log.Error("获取用户数据失败:%s", err)
return protocol.NewErrWithMessage("1")
}
var (
ids []int64
)
for _, v := range updateIds {
ids = append(ids, v.Id)
}
num, err := o.QueryTable(&models.UserCompany{}).
Filter("delete_at", 0).
Filter("company_id", companyid).
Filter("id__in", ids).
Update(orm.Params{
"enable": models.USERCOMPANY_ENABLE_NO,
"update_at": time.Now(),
})
if err != nil {
log.Error("更新user_company失败:%s", err)
protocol.NewErrWithMessage("1")
}
log.Info("更新记录数:%d", num)
for _, v := range updateIds {
e := redisdata.DeleteLoginToken(v.UserId)
if e != nil {
log.Error(e.Error())
}
}
return nil
}
//启用用户
func UserAllow(userCompanyIds []int64, companyid int64) error {
if len(userCompanyIds) == 0 {
return nil
}
o := orm.NewOrm()
num, err := o.QueryTable(&models.UserCompany{}).
Filter("delete_at", 0).
Filter("company_id", companyid).
Filter("id__in", userCompanyIds).
Update(orm.Params{
"enable": enable,
"enable": models.USERCOMPANY_ENABLE_YES,
"update_at": time.Now(),
})
if err != nil {
... ...
package rbac
import (
"oppmg/common/log"
"oppmg/models"
"oppmg/protocol"
"oppmg/utils"
)
//获取全部的权限菜单
func GetMenuAll() ([]protocol.PermissionItem, error) {
const datasql string = `SELECT id,name,icon,parent_id,senior_status,sort
FROM menu WHERE enabled=1 ORDER BY sort `
var (
list []protocol.PermissionItem
err error
)
err = utils.ExecuteQueryAll(&list, datasql)
if err != nil {
log.Error("EXECUTE SQL err:%s", err)
return nil, protocol.NewErrWithMessage("1")
}
return list, nil
}
//
func GetRoleHasMenu(roleid int64, companyid int64) (*protocol.ResponseRoleMenus, error) {
var (
roleData *models.Role
err error
)
roleData, err = models.GetRoleById(roleid)
if err != nil {
log.Error("获取角色数据失败:%s", err)
return nil, err
}
if roleData.Types != models.ROLETYPES_ROLE {
log.Error("角色类型错误")
return nil, protocol.NewErrWithMessage("1")
}
if roleData.CompanyId != companyid {
log.Error("角色的公司id不匹配")
return nil, protocol.NewErrWithMessage("1")
}
var (
rolemenu []models.RoleMenu
ids []int64
)
rolemenu, err = models.GetRoleMenuByRole(roleid)
if err != nil {
log.Error("获取角色下的菜单数据失败:%s", err)
return nil, protocol.NewErrWithMessage("1")
}
for _, v := range rolemenu {
ids = append(ids, v.MenuId)
}
data := &protocol.ResponseRoleMenus{
RoleId: roleData.Id,
RoleName: roleData.Name,
MenuId: ids,
}
return data, nil
}
func RoleMenuEdit(companyid int64, roleid int64, menuids []int64) {
}
... ...