-
Notifications
You must be signed in to change notification settings - Fork 44
/
keeper.go
230 lines (190 loc) · 8.15 KB
/
keeper.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
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
package keeper
import (
"github.com/cosmos/cosmos-sdk/codec"
sdk "github.com/cosmos/cosmos-sdk/types"
sdkerrors "github.com/cosmos/cosmos-sdk/types/errors"
"github.com/tendermint/tendermint/libs/log"
"github.com/desmos-labs/desmos/v2/x/staging/subspaces/types"
)
type Keeper struct {
storeKey sdk.StoreKey
cdc codec.BinaryCodec
}
// NewKeeper creates new instances of the subspaces keeper
func NewKeeper(storeKey sdk.StoreKey, cdc codec.BinaryCodec) Keeper {
return Keeper{
storeKey: storeKey,
cdc: cdc,
}
}
// Logger returns a module-specific logger.
func (k Keeper) Logger(ctx sdk.Context) log.Logger {
return ctx.Logger().With("module", "x/"+types.ModuleName)
}
// SaveSubspace saves the given subspace inside the current context.
func (k Keeper) SaveSubspace(ctx sdk.Context, subspace types.Subspace, user string) error {
err := subspace.Validate()
if err != nil {
return err
}
storedSubspace, found := k.GetSubspace(ctx, subspace.ID)
// Check the editor when the user is trying to edit the subspace
if found && storedSubspace.Owner != user {
return sdkerrors.Wrapf(types.ErrInvalidSubspaceOwner, user)
}
store := ctx.KVStore(k.storeKey)
key := types.SubspaceStoreKey(subspace.ID)
store.Set(key, k.cdc.MustMarshal(&subspace))
k.Logger(ctx).Info("saved subspace", "id", subspace.ID, "owner", subspace.Owner)
return nil
}
// DoesSubspaceExist checks if the subspace with the given id exists.
func (k Keeper) DoesSubspaceExist(ctx sdk.Context, subspaceID string) bool {
store := ctx.KVStore(k.storeKey)
return store.Has(types.SubspaceStoreKey(subspaceID))
}
// GetSubspace returns the subspace associated with the given ID.
// If there is no subspace associated with the given ID the function will return an empty subspace and false.
func (k Keeper) GetSubspace(ctx sdk.Context, subspaceID string) (subspace types.Subspace, found bool) {
if !k.DoesSubspaceExist(ctx, subspaceID) {
return types.Subspace{}, false
}
store := ctx.KVStore(k.storeKey)
k.cdc.MustUnmarshal(store.Get(types.SubspaceStoreKey(subspaceID)), &subspace)
return subspace, true
}
// IsAdmin returns true iff the given user is an admin of the subspace with the given id
func (k Keeper) IsAdmin(ctx sdk.Context, subspaceID string, user string) bool {
store := ctx.KVStore(k.storeKey)
return store.Has(types.SubspaceAdminKey(subspaceID, user))
}
// AddAdminToSubspace sets the given user as an admin of the subspace having the given id.
// Returns an error if the user is already an admin or the subspace does not exist.
func (k Keeper) AddAdminToSubspace(ctx sdk.Context, subspaceID, user, owner string) error {
// Check if the subspace exists and the owner is valid
err := k.checkSubspaceOwner(ctx, subspaceID, owner)
if err != nil {
return err
}
// Check if the user we want to set as admin is already an admin
if k.IsAdmin(ctx, subspaceID, user) {
return sdkerrors.Wrapf(sdkerrors.ErrInvalidRequest, "the user with address %s is already an admin", user)
}
// Store the admin
store := ctx.KVStore(k.storeKey)
store.Set(types.SubspaceAdminKey(subspaceID, user), []byte(user))
k.Logger(ctx).Info("added admin", "subspace-id", subspaceID, "admin", user)
return nil
}
// RemoveAdminFromSubspace removes the given user from the admin set of the subspace having the given id.
// It returns an error if the user was not an admin or the subspace does not exist.
func (k Keeper) RemoveAdminFromSubspace(ctx sdk.Context, subspaceID, user, owner string) error {
// Check if the subspace exists and the owner is valid
err := k.checkSubspaceOwner(ctx, subspaceID, owner)
if err != nil {
return err
}
// Check if the user is not an admin
if !k.IsAdmin(ctx, subspaceID, user) {
return sdkerrors.Wrapf(types.ErrInvalidSubspaceAdmin, user)
}
// Delete the admin
store := ctx.KVStore(k.storeKey)
store.Delete(types.SubspaceAdminKey(subspaceID, user))
k.Logger(ctx).Info("removed admin", "subspace-id", subspaceID, "admin", user)
return nil
}
// IsRegistered returns true iff the given user is registered inside the subspace with the given id
func (k Keeper) IsRegistered(ctx sdk.Context, subspaceID, user string) bool {
store := ctx.KVStore(k.storeKey)
return store.Has(types.SubspaceRegisteredUserKey(subspaceID, user))
}
// RegisterUserInSubspace registers the given user inside the subspace with the given ID.
// It returns error if the user is already registered or the subspace does not exist.
func (k Keeper) RegisterUserInSubspace(ctx sdk.Context, subspaceID, user, admin string) error {
// Check if the subspace exists and the admin is an actual admin
err := k.checkSubspaceAdmin(ctx, subspaceID, admin)
if err != nil {
return err
}
// Check if the user is already registered inside the subspace
if k.IsRegistered(ctx, subspaceID, user) {
return sdkerrors.Wrapf(sdkerrors.ErrInvalidRequest, "user already registered")
}
// Store the new user
store := ctx.KVStore(k.storeKey)
store.Set(types.SubspaceRegisteredUserKey(subspaceID, user), []byte(user))
return nil
}
// UnregisterUserFromSubspace unregisters the given user from the subspace with the given ID.
// It returns error if the user is not registered or the subspace does not exist.
func (k Keeper) UnregisterUserFromSubspace(ctx sdk.Context, subspaceID, user, admin string) error {
// Check if the subspace exists and the admin is an actual admin
err := k.checkSubspaceAdmin(ctx, subspaceID, admin)
if err != nil {
return err
}
// Check if the user is already registered inside the subspace
if !k.IsRegistered(ctx, subspaceID, user) {
return sdkerrors.Wrapf(sdkerrors.ErrInvalidRequest,
"the user with address : %s is not registered inside the subspace: %s", user, subspaceID)
}
// Remove the user
store := ctx.KVStore(k.storeKey)
store.Delete(types.SubspaceRegisteredUserKey(subspaceID, user))
return nil
}
// IsBanned returns true iff the given user is banned inside the subspace with the given id
func (k Keeper) IsBanned(ctx sdk.Context, subspaceID, user string) bool {
store := ctx.KVStore(k.storeKey)
return store.Has(types.SubspaceBannedUserKey(subspaceID, user))
}
// BanUserInSubspace bans the given user inside the subspace with the given ID.
// It returns and error if the user is already blocked inside the subspace or the subspace does not exist.
func (k Keeper) BanUserInSubspace(ctx sdk.Context, subspaceID, user, admin string) error {
// Check if the subspace exists and the admin is an actual admin
err := k.checkSubspaceAdmin(ctx, subspaceID, admin)
if err != nil {
return err
}
// Check if the user is already banned inside the subspace
if k.IsBanned(ctx, subspaceID, user) {
return sdkerrors.Wrapf(sdkerrors.ErrInvalidRequest, "user already banned")
}
// Store the banned user
store := ctx.KVStore(k.storeKey)
store.Set(types.SubspaceBannedUserKey(subspaceID, user), []byte(user))
return nil
}
// UnbanUserInSubspace unbans the given user inside the subspace with the given ID.
// It returns error if the user is not banned inside the subspace or the subspace does not exist.
func (k Keeper) UnbanUserInSubspace(ctx sdk.Context, subspaceID, user, admin string) error {
// Check if the subspace exists and the admin is an actual admin
err := k.checkSubspaceAdmin(ctx, subspaceID, admin)
if err != nil {
return err
}
// Check if the user is already banned inside the subspace
if !k.IsBanned(ctx, subspaceID, user) {
return sdkerrors.Wrapf(sdkerrors.ErrInvalidRequest, "the user is not blocked inside the subspace")
}
// Remove the banned user
store := ctx.KVStore(k.storeKey)
store.Delete(types.SubspaceBannedUserKey(subspaceID, user))
return nil
}
// CheckSubspaceUserPermission checks the permission of the given user inside the subspace with the
// given id to make sure they are able to perform operations inside it
func (k Keeper) CheckSubspaceUserPermission(ctx sdk.Context, subspaceID string, user string) error {
subspace, found := k.GetSubspace(ctx, subspaceID)
if !found {
return sdkerrors.Wrapf(types.ErrInvalidSubspaceID, subspaceID)
}
if k.IsBanned(ctx, subspaceID, user) {
return sdkerrors.Wrapf(types.ErrPermissionDenied, user)
}
if subspace.Type == types.SubspaceTypeClosed && !k.IsRegistered(ctx, subspaceID, user) {
return sdkerrors.Wrapf(types.ErrPermissionDenied, user)
}
return nil
}