-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add migration to AddUserMember interactor [FLOW-BE-34] (#74)
* add role and permittable domain * add role and permittable mongo * add role and permittable mongo * add role and permittable accountrepo * add role and permittable accountrepo * add migration to AddUserMember interactor * add role and permittable accountmemory * add TestWorkspace_AddMember_Migration * add comment * fix comment * fix Filtered * fix Filtered * Revert "fix Filtered" This reverts commit 2063494. * Revert "fix Filtered" This reverts commit 2425d89.
- Loading branch information
1 parent
7860571
commit 6f054a7
Showing
21 changed files
with
1,096 additions
and
12 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
// TODO: Delete this file once the permission check migration is complete. | ||
|
||
package permittable | ||
|
||
import ( | ||
"github.com/reearth/reearthx/account/accountdomain" | ||
"github.com/reearth/reearthx/account/accountdomain/user" | ||
) | ||
|
||
type Builder struct { | ||
p *Permittable | ||
} | ||
|
||
func New() *Builder { | ||
return &Builder{p: &Permittable{}} | ||
} | ||
|
||
func (b *Builder) Build() (*Permittable, error) { | ||
if b.p.id.IsNil() { | ||
return nil, ErrInvalidID | ||
} | ||
if b.p.userID.IsNil() { | ||
return nil, ErrInvalidID | ||
} | ||
return b.p, nil | ||
} | ||
|
||
func (b *Builder) MustBuild() *Permittable { | ||
u, err := b.Build() | ||
if err != nil { | ||
panic(err) | ||
} | ||
return u | ||
} | ||
|
||
func (b *Builder) ID(id ID) *Builder { | ||
b.p.id = id | ||
return b | ||
} | ||
|
||
func (b *Builder) NewID() *Builder { | ||
b.p.id = NewID() | ||
return b | ||
} | ||
|
||
func (b *Builder) UserID(userID user.ID) *Builder { | ||
b.p.userID = userID | ||
return b | ||
} | ||
|
||
func (b *Builder) RoleIDs(roleIDs []accountdomain.RoleID) *Builder { | ||
b.p.roleIDs = roleIDs | ||
return b | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
// TODO: Delete this file once the permission check migration is complete. | ||
|
||
package permittable | ||
|
||
import "github.com/reearth/reearthx/account/accountdomain" | ||
|
||
type ID = accountdomain.PermittableID | ||
|
||
var NewID = accountdomain.NewPermittableID | ||
|
||
var MustID = accountdomain.MustPermittableID | ||
|
||
var IDFrom = accountdomain.PermittableIDFrom | ||
|
||
var IDFromRef = accountdomain.PermittableIDFromRef | ||
|
||
var ErrInvalidID = accountdomain.ErrInvalidID |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
// TODO: Delete this file once the permission check migration is complete. | ||
|
||
package permittable | ||
|
||
type List []*Permittable |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
// TODO: Delete this file once the permission check migration is complete. | ||
|
||
package permittable | ||
|
||
import ( | ||
"github.com/reearth/reearthx/account/accountdomain" | ||
"github.com/reearth/reearthx/account/accountdomain/role" | ||
"github.com/reearth/reearthx/account/accountdomain/user" | ||
) | ||
|
||
type Permittable struct { | ||
id ID | ||
userID user.ID | ||
roleIDs []role.ID | ||
} | ||
|
||
func (p *Permittable) ID() ID { | ||
if p == nil { | ||
return ID{} | ||
} | ||
return p.id | ||
} | ||
|
||
func (p *Permittable) UserID() user.ID { | ||
if p == nil { | ||
return user.ID{} | ||
} | ||
return p.userID | ||
} | ||
|
||
func (p *Permittable) RoleIDs() []accountdomain.RoleID { | ||
if p == nil { | ||
return nil | ||
} | ||
return p.roleIDs | ||
} | ||
|
||
func (p *Permittable) EditRoleIDs(roleIDs accountdomain.RoleIDList) { | ||
if p == nil { | ||
return | ||
} | ||
p.roleIDs = roleIDs | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
// TODO: Delete this file once the permission check migration is complete. | ||
|
||
package role | ||
|
||
type Builder struct { | ||
r *Role | ||
} | ||
|
||
func New() *Builder { | ||
return &Builder{r: &Role{}} | ||
} | ||
|
||
func (b *Builder) Build() (*Role, error) { | ||
if b.r.id.IsNil() { | ||
return nil, ErrInvalidID | ||
} | ||
if b.r.name == "" { | ||
return nil, ErrEmptyName | ||
} | ||
return b.r, nil | ||
} | ||
|
||
func (b *Builder) MustBuild() *Role { | ||
g, err := b.Build() | ||
if err != nil { | ||
panic(err) | ||
} | ||
return g | ||
} | ||
|
||
func (b *Builder) ID(id ID) *Builder { | ||
b.r.id = id | ||
return b | ||
} | ||
|
||
func (b *Builder) NewID() *Builder { | ||
b.r.id = NewID() | ||
return b | ||
} | ||
|
||
func (b *Builder) Name(name string) *Builder { | ||
b.r.name = name | ||
return b | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
// TODO: Delete this file once the permission check migration is complete. | ||
|
||
package role | ||
|
||
import ( | ||
"github.com/reearth/reearthx/account/accountdomain" | ||
) | ||
|
||
type ID = accountdomain.RoleID | ||
|
||
var NewID = accountdomain.NewRoleID | ||
|
||
var MustID = accountdomain.MustRoleID | ||
|
||
var IDFrom = accountdomain.RoleIDFrom | ||
|
||
var IDFromRef = accountdomain.RoleIDFromRef | ||
|
||
var ErrInvalidID = accountdomain.ErrInvalidID |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
// TODO: Delete this file once the permission check migration is complete. | ||
|
||
package role | ||
|
||
type List []*Role |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
// TODO: Delete this file once the permission check migration is complete. | ||
|
||
package role | ||
|
||
import "errors" | ||
|
||
var ( | ||
ErrEmptyName = errors.New("role name can't be empty") | ||
) | ||
|
||
type Role struct { | ||
id ID | ||
name string | ||
} | ||
|
||
func (r *Role) ID() ID { | ||
if r == nil { | ||
return ID{} | ||
} | ||
return r.id | ||
} | ||
|
||
func (r *Role) Name() string { | ||
if r == nil { | ||
return "" | ||
} | ||
return r.name | ||
} | ||
|
||
func (r *Role) Rename(name string) { | ||
if r == nil { | ||
return | ||
} | ||
r.name = name | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
93 changes: 93 additions & 0 deletions
93
account/accountinfrastructure/accountmemory/permittable.go
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,93 @@ | ||
// TODO: Delete this file once the permission check migration is complete. | ||
|
||
package accountmemory | ||
|
||
import ( | ||
"context" | ||
"slices" | ||
"sync" | ||
|
||
"github.com/reearth/reearthx/account/accountdomain" | ||
"github.com/reearth/reearthx/account/accountdomain/permittable" | ||
"github.com/reearth/reearthx/account/accountdomain/user" | ||
"github.com/reearth/reearthx/rerror" | ||
) | ||
|
||
type Permittable struct { | ||
lock sync.Mutex | ||
data map[accountdomain.PermittableID]*permittable.Permittable | ||
} | ||
|
||
func NewPermittable() *Permittable { | ||
return &Permittable{ | ||
data: map[accountdomain.PermittableID]*permittable.Permittable{}, | ||
} | ||
} | ||
|
||
func NewPermittableWith(items ...*permittable.Permittable) *Permittable { | ||
p := NewPermittable() | ||
ctx := context.Background() | ||
for _, i := range items { | ||
_ = p.Save(ctx, *i) | ||
} | ||
return p | ||
} | ||
|
||
func (p *Permittable) FindByUserID(ctx context.Context, userID user.ID) (*permittable.Permittable, error) { | ||
p.lock.Lock() | ||
defer p.lock.Unlock() | ||
|
||
for _, perm := range p.data { | ||
if perm.UserID() == userID { | ||
return perm, nil | ||
} | ||
} | ||
return nil, rerror.ErrNotFound | ||
} | ||
|
||
func (p *Permittable) FindByUserIDs(ctx context.Context, userIDs user.IDList) (permittable.List, error) { | ||
p.lock.Lock() | ||
defer p.lock.Unlock() | ||
|
||
results := make(permittable.List, 0, len(userIDs)) | ||
for _, userID := range userIDs { | ||
for _, perm := range p.data { | ||
if perm.UserID() == userID { | ||
results = append(results, perm) | ||
break | ||
} | ||
} | ||
} | ||
|
||
if len(results) == 0 { | ||
return nil, rerror.ErrNotFound | ||
} | ||
|
||
return results, nil | ||
} | ||
|
||
func (p *Permittable) FindByRoleID(ctx context.Context, roleID accountdomain.RoleID) (permittable.List, error) { | ||
p.lock.Lock() | ||
defer p.lock.Unlock() | ||
|
||
results := make(permittable.List, 0, len(p.data)) | ||
for _, perm := range p.data { | ||
if slices.Contains(perm.RoleIDs(), roleID) { | ||
results = append(results, perm) | ||
} | ||
} | ||
|
||
if len(results) == 0 { | ||
return nil, rerror.ErrNotFound | ||
} | ||
|
||
return results, nil | ||
} | ||
|
||
func (r *Permittable) Save(ctx context.Context, p permittable.Permittable) error { | ||
r.lock.Lock() | ||
defer r.lock.Unlock() | ||
|
||
r.data[p.ID()] = &p | ||
return nil | ||
} |
Oops, something went wrong.