Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(examples): finalize acl package #2987

Open
wants to merge 12 commits into
base: master
Choose a base branch
from
172 changes: 163 additions & 9 deletions examples/gno.land/p/demo/acl/acl.gno
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,20 @@ func (d *Directory) AddUserPerm(addr std.Address, verb, resource string) {
verbs: []string{verb},
resources: []string{resource},
}
d.addPermToBucket(bucket, p)
d.addPermsToBucket(bucket, []perm{p})
}

func (d *Directory) AddUserPerms(addr std.Address, verbs []string, resource string) {
bucket := "u:" + addr.String()
var ps perms
for _, verb := range verbs {
p := perm{
verbs: []string{verb},
resources: []string{resource},
}
ps = append(ps, p)
}
d.addPermsToBucket(bucket, ps)
}

func (d *Directory) AddGroupPerm(name string, verb, resource string) {
Expand All @@ -71,17 +84,30 @@ func (d *Directory) AddGroupPerm(name string, verb, resource string) {
verbs: []string{verb},
resources: []string{resource},
}
d.addPermToBucket(bucket, p)
d.addPermsToBucket(bucket, []perm{p})
}

func (d *Directory) AddGroupPerms(name string, verbs []string, resource string) {
bucket := "g:" + name
var ps perms
for _, verb := range verbs {
p := perm{
verbs: []string{verb},
resources: []string{resource},
}
ps = append(ps, p)
}
d.addPermsToBucket(bucket, ps)
}

func (d *Directory) addPermToBucket(bucket string, p perm) {
func (d *Directory) addPermsToBucket(bucket string, p perms) {
var ps perms

existing, ok := d.permBuckets.Get(bucket)
if ok {
ps = existing.(perms)
}
ps = append(ps, p)
ps = ps.appendWithoutDuplicates(p)

d.permBuckets.Set(bucket, ps)
}
Expand All @@ -92,11 +118,139 @@ func (d *Directory) AddUserToGroup(user std.Address, group string) {
if ok {
groups = existing.([]string)
}
groups = append(groups, group)
groups = appendGroupsWithoutDuplicates(groups, []string{group})
d.userGroups.Set(user.String(), groups)
}

func (d *Directory) AddUserToGroups(user std.Address, newGroups []string) {
existing, ok := d.userGroups.Get(user.String())
var groups []string
if ok {
groups = existing.([]string)
}
groups = appendGroupsWithoutDuplicates(groups, newGroups)
d.userGroups.Set(user.String(), groups)
}

// TODO: helpers to remove permissions.
// TODO: helpers to adds multiple permissions at once -> {verbs: []string{"read","write"}}.
// TODO: helpers to delete users from gorups.
// TODO: helpers to quickly reset states.
func (d *Directory) RemoveUserFromGroup(user std.Address, group string) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should we return an error or a boolean indicating success when removing a field?

existing, ok := d.userGroups.Get(user.String())
if !ok {
return
}
groups := existing.([]string)
for i, g := range groups {
if g == group {
groups = append(groups[:i], groups[i+1:]...)
break
}
}

if len(groups) == 0 {
d.userGroups.Remove(user.String())
} else {
d.userGroups.Set(user.String(), groups)
}
}

func (d *Directory) RemoveUserFromGroups(user std.Address, groups []string) {
existing, ok := d.userGroups.Get(user.String())
if !ok {
return
}
existingGroups := existing.([]string)
for _, group := range groups {
for i, g := range existingGroups {
if g == group {
existingGroups = append(existingGroups[:i], existingGroups[i+1:]...)
break
}
}
}

if len(existingGroups) == 0 {
d.userGroups.Remove(user.String())
} else {
d.userGroups.Set(user.String(), existingGroups)
}
}

func (d *Directory) removePermsFromBucket(bucket string, p []perm) {
existing, ok := d.permBuckets.Get(bucket)
if !ok {
return
}
ps := existing.(perms)

for _, perm := range p {
for i, existingPerm := range ps {
if perm.Equal(existingPerm) {
ps = append(ps[:i], ps[i+1:]...)
break
}
}
}

if len(ps) == 0 {
d.permBuckets.Remove(bucket)
} else {
d.permBuckets.Set(bucket, ps)
}
}

func (d *Directory) RemoveUserPerm(addr std.Address, verb, resource string) {
bucket := "u:" + addr.String()
p := perm{
verbs: []string{verb},
resources: []string{resource},
}
d.removePermsFromBucket(bucket, []perm{p})
}

func (d *Directory) RemoveUserPerms(addr std.Address, verbs []string, resource string) {
bucket := "u:" + addr.String()
var ps perms
for _, verb := range verbs {
p := perm{
verbs: []string{verb},
resources: []string{resource},
}
ps = append(ps, p)
}
d.removePermsFromBucket(bucket, ps)
}

func (d *Directory) RemoveGroupPerm(name string, verb, resource string) {
bucket := "g:" + name
p := perm{
verbs: []string{verb},
resources: []string{resource},
}
d.removePermsFromBucket(bucket, []perm{p})
}

func (d *Directory) RemoveGroupPerms(name string, verbs []string, resource string) {
bucket := "g:" + name
var ps perms
for _, verb := range verbs {
p := perm{
verbs: []string{verb},
resources: []string{resource},
}
ps = append(ps, p)
}
d.removePermsFromBucket(bucket, ps)
}

func (d *Directory) ResetGroupPerms(name string) {
bucket := "g:" + name
d.permBuckets.Remove(bucket)
}

func (d *Directory) ResetUserPerms(addr std.Address) {
bucket := "u:" + addr.String()
d.permBuckets.Remove(bucket)
}

func (d *Directory) ResetUserGroups(addr std.Address) {
d.userGroups.Remove(addr.String())
}
Loading