-
Notifications
You must be signed in to change notification settings - Fork 1
/
role_permission.go
78 lines (67 loc) · 2.24 KB
/
role_permission.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
/*
Nging is a toolbox for webmasters
Copyright (C) 2018-present Wenhui Shen <swh@admpub.com>
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU Affero General Public License as published
by the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU Affero General Public License for more details.
You should have received a copy of the GNU Affero General Public License
along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
package customer
import (
"github.com/webx-top/db"
"github.com/webx-top/echo"
"github.com/webx-top/echo/code"
"github.com/admpub/webx/application/dbschema"
)
func NewRolePermission(ctx echo.Context) *RolePermission {
return &RolePermission{
OfficialCustomerRolePermission: dbschema.NewOfficialCustomerRolePermission(ctx),
}
}
type RolePermission struct {
*dbschema.OfficialCustomerRolePermission
}
func (u *RolePermission) check() error {
if len(u.Type) == 0 {
return u.Context().NewError(code.InvalidParameter, `权限类型不能为空`).SetZone(`type`)
}
if u.RoleId < 1 {
return u.Context().NewError(code.InvalidParameter, `权限的角色ID无效`).SetZone(`roleId`)
}
return nil
}
func (u *RolePermission) Add() (interface{}, error) {
if err := u.check(); err != nil {
return nil, err
}
exists, err := u.Exists(u.RoleId, u.Type)
if err != nil {
return nil, err
}
if exists {
err = u.UpdateFields(nil, echo.H{`permission`: u.Permission}, db.And(
db.Cond{`role_id`: u.RoleId},
db.Cond{`type`: u.Type},
))
return nil, err
}
return u.OfficialCustomerRolePermission.Insert()
}
func (u *RolePermission) Edit(mw func(db.Result) db.Result, args ...interface{}) error {
if err := u.check(); err != nil {
return err
}
return u.OfficialCustomerRolePermission.Update(mw, args...)
}
func (u *RolePermission) Exists(roleID uint, typ string) (bool, error) {
return u.OfficialCustomerRolePermission.Exists(nil, db.And(
db.Cond{`role_id`: roleID},
db.Cond{`type`: typ},
))
}