-
Notifications
You must be signed in to change notification settings - Fork 10
/
auth.go
85 lines (67 loc) · 2.51 KB
/
auth.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
79
80
81
82
83
84
85
package keeper
import (
gogotypes "github.com/gogo/protobuf/types"
sdk "github.com/cosmos/cosmos-sdk/types"
"github.com/bianjieai/iritamod/modules/perm/types"
)
// SetAuth sets the auth for an address
func (k Keeper) SetAuth(ctx sdk.Context, address sdk.AccAddress, auth types.Auth) {
store := ctx.KVStore(k.storeKey)
bz := k.cdc.MustMarshal(&gogotypes.Int32Value{Value: int32(auth)})
store.Set(types.GetAuthKey(address), bz)
}
// DeleteAuth deletes the auth for an address
func (k Keeper) DeleteAuth(ctx sdk.Context, address sdk.AccAddress) {
store := ctx.KVStore(k.storeKey)
store.Delete(types.GetAuthKey(address))
}
// GetAuth gets the auth for an address
func (k Keeper) GetAuth(ctx sdk.Context, address sdk.AccAddress) types.Auth {
store := ctx.KVStore(k.storeKey)
value := store.Get(types.GetAuthKey(address))
if value == nil {
return 0
}
var role gogotypes.Int32Value
k.cdc.MustUnmarshal(value, &role)
return types.Auth(role.Value)
}
func (k Keeper) IsRootAdmin(ctx sdk.Context, address sdk.AccAddress) bool {
auth := k.GetAuth(ctx, address)
return (auth & types.RoleRootAdmin.Auth()) > 0
}
func (k Keeper) IsPermAdmin(ctx sdk.Context, address sdk.AccAddress) bool {
auth := k.GetAuth(ctx, address)
return (auth & types.RolePermAdmin.Auth()) > 0
}
func (k Keeper) IsBaseM1Admin(ctx sdk.Context, address sdk.AccAddress) bool {
auth := k.GetAuth(ctx, address)
return (auth & types.RoleBaseM1Admin.Auth()) > 0
}
func (k Keeper) IsPlatformUser(ctx sdk.Context, address sdk.AccAddress) bool {
auth := k.GetAuth(ctx, address)
return (auth & types.RolePlatformUser.Auth()) > 0
}
func (k Keeper) IsPowerUserAdmin(ctx sdk.Context, address sdk.AccAddress) bool {
auth := k.GetAuth(ctx, address)
return (auth & types.RolePowerUserAdmin.Auth()) > 0
}
func (k Keeper) IsAdmin(ctx sdk.Context, address sdk.AccAddress) bool {
auth := k.GetAuth(ctx, address)
return (auth&types.RoleRootAdmin.Auth()) > 0 ||
(auth&types.RolePermAdmin.Auth()) > 0 ||
(auth&types.RoleBlacklistAdmin.Auth()) > 0 ||
(auth&types.RoleNodeAdmin.Auth()) > 0 ||
(auth&types.RoleParamAdmin.Auth()) > 0
}
func (k Keeper) IsAdminPerm(ctx sdk.Context, address sdk.AccAddress) bool {
auth := k.GetAuth(ctx, address)
return (auth&types.RoleRootAdmin.Auth()) > 0 ||
(auth&types.RolePermAdmin.Auth()) > 0
}
func (k Keeper) IsPowerAdminPerm(ctx sdk.Context, address sdk.AccAddress) bool {
auth := k.GetAuth(ctx, address)
return (auth&types.RoleRootAdmin.Auth()) > 0 ||
(auth&types.RolePermAdmin.Auth()) > 0 ||
(auth&types.RolePowerUserAdmin.Auth()) > 0
}