2017-09-26 12:50:09 +00:00
|
|
|
package biz
|
|
|
|
|
|
|
|
import (
|
2021-12-06 12:24:22 +00:00
|
|
|
"context"
|
2017-09-26 12:50:09 +00:00
|
|
|
|
|
|
|
"github.com/cuigh/auxo/net/web"
|
|
|
|
"github.com/cuigh/swirl/dao"
|
|
|
|
"github.com/cuigh/swirl/model"
|
|
|
|
)
|
|
|
|
|
2021-12-06 12:24:22 +00:00
|
|
|
type RoleBiz interface {
|
2021-12-16 12:23:08 +00:00
|
|
|
Search(name string) ([]*model.Role, error)
|
|
|
|
Find(id string) (role *model.Role, err error)
|
|
|
|
Create(role *model.Role, user web.User) (err error)
|
2021-12-06 12:24:22 +00:00
|
|
|
Delete(id, name string, user web.User) (err error)
|
2021-12-16 12:23:08 +00:00
|
|
|
Update(r *model.Role, user web.User) (err error)
|
2021-12-22 09:43:26 +00:00
|
|
|
GetPerms(ids []string) ([]string, error)
|
2017-09-26 12:50:09 +00:00
|
|
|
}
|
|
|
|
|
2021-12-06 12:24:22 +00:00
|
|
|
func NewRole(d dao.Interface, eb EventBiz) RoleBiz {
|
|
|
|
return &roleBiz{d: d, eb: eb}
|
|
|
|
}
|
|
|
|
|
|
|
|
type roleBiz struct {
|
|
|
|
d dao.Interface
|
|
|
|
eb EventBiz
|
|
|
|
}
|
|
|
|
|
2021-12-16 12:23:08 +00:00
|
|
|
func (b *roleBiz) Search(name string) (roles []*model.Role, err error) {
|
|
|
|
return b.d.RoleSearch(context.TODO(), name)
|
2017-09-26 12:50:09 +00:00
|
|
|
}
|
|
|
|
|
2021-12-16 12:23:08 +00:00
|
|
|
func (b *roleBiz) Create(role *model.Role, user web.User) (err error) {
|
2021-12-06 12:24:22 +00:00
|
|
|
r := &model.Role{
|
|
|
|
ID: createId(),
|
|
|
|
Name: role.Name,
|
|
|
|
Description: role.Description,
|
|
|
|
Perms: role.Perms,
|
2021-12-16 12:23:08 +00:00
|
|
|
CreatedAt: now(),
|
|
|
|
CreatedBy: newOperator(user),
|
2021-12-06 12:24:22 +00:00
|
|
|
}
|
|
|
|
r.UpdatedAt = r.CreatedAt
|
2021-12-16 12:23:08 +00:00
|
|
|
r.UpdatedBy = r.CreatedBy
|
2021-12-06 12:24:22 +00:00
|
|
|
err = b.d.RoleCreate(context.TODO(), r)
|
|
|
|
if err == nil {
|
|
|
|
b.eb.CreateRole(EventActionCreate, r.ID, role.Name, user)
|
|
|
|
}
|
|
|
|
return
|
|
|
|
}
|
2017-09-26 12:50:09 +00:00
|
|
|
|
2021-12-06 12:24:22 +00:00
|
|
|
func (b *roleBiz) Delete(id, name string, user web.User) (err error) {
|
|
|
|
err = b.d.RoleDelete(context.TODO(), id)
|
|
|
|
if err == nil {
|
|
|
|
b.eb.CreateRole(EventActionDelete, id, name, user)
|
|
|
|
}
|
2017-09-26 12:50:09 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2021-12-16 12:23:08 +00:00
|
|
|
func (b *roleBiz) Find(id string) (role *model.Role, err error) {
|
|
|
|
return b.d.RoleGet(context.TODO(), id)
|
2017-09-26 12:50:09 +00:00
|
|
|
}
|
|
|
|
|
2021-12-16 12:23:08 +00:00
|
|
|
func (b *roleBiz) Update(role *model.Role, user web.User) (err error) {
|
2021-12-06 12:24:22 +00:00
|
|
|
r := &model.Role{
|
|
|
|
ID: role.ID,
|
|
|
|
Name: role.Name,
|
|
|
|
Description: role.Description,
|
|
|
|
Perms: role.Perms,
|
2021-12-16 12:23:08 +00:00
|
|
|
UpdatedAt: now(),
|
|
|
|
UpdatedBy: newOperator(user),
|
2021-12-06 12:24:22 +00:00
|
|
|
}
|
|
|
|
err = b.d.RoleUpdate(context.TODO(), r)
|
|
|
|
if err == nil {
|
|
|
|
b.eb.CreateRole(EventActionUpdate, role.ID, role.Name, user)
|
|
|
|
}
|
2017-09-26 12:50:09 +00:00
|
|
|
return
|
|
|
|
}
|
2021-12-22 09:43:26 +00:00
|
|
|
|
|
|
|
func (b *roleBiz) GetPerms(ids []string) ([]string, error) {
|
|
|
|
m := make(map[string]struct{})
|
|
|
|
|
|
|
|
for _, id := range ids {
|
|
|
|
r, err := b.d.RoleGet(context.TODO(), id)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, p := range r.Perms {
|
|
|
|
m[p] = struct{}{}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
perms := make([]string, 0, len(m))
|
|
|
|
for p := range m {
|
|
|
|
perms = append(perms, p)
|
|
|
|
}
|
|
|
|
return perms, nil
|
|
|
|
}
|