Skip to content

Commit

Permalink
Add UpdateGroupRequest
Browse files Browse the repository at this point in the history
  • Loading branch information
Laica-Lunasys committed Jun 16, 2024
1 parent 40e74c9 commit bed4005
Show file tree
Hide file tree
Showing 5 changed files with 353 additions and 185 deletions.
30 changes: 30 additions & 0 deletions database/group.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,11 +31,20 @@ func (g *Groups) ToProtobuf() *systerapb.GroupEntry {
}

permsMap := make(map[string][]string)
perms := []*systerapb.PermissionsEntry{}

for _, p := range g.Permissions {
permsMap[p.ServerName] = append(permsMap[p.ServerName], p.Permission)
}

for serverName, permissions := range permsMap {
perms = append(perms, &systerapb.PermissionsEntry{
ServerName: serverName,
Permissions: permissions,
})
}

e.Permissions = perms
return e
}

Expand Down Expand Up @@ -91,6 +100,27 @@ func (s *Mysql) RemoveGroup(groupName string) error {
return nil
}

// UpdateGroup - Update Group
func (s *Mysql) UpdateGroup(newGroup Groups) error {
group := Groups{}
r := s.client.First(&group, "name = ?", newGroup.Name)

if r.RowsAffected == 0 {
return errors.New("group does not exists")
} else if r.Error != nil && r.RowsAffected != 0 {
return r.Error
}
newGroup.ID = group.ID

result := r.Save(&newGroup)

if result.Error != nil {
return result.Error
}

return nil
}

// AddPermission - Add Permission
func (s *Mysql) AddPermission(groupName, target string, permissions []string) error {
var group Groups
Expand Down
29 changes: 28 additions & 1 deletion server/group.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ func (s *grpcServer) CreateGroup(ctx context.Context, e *pb.CreateGroupRequest)
return &pb.Empty{}, err
}

stream.PublishGroup(d.ToProtobuf())
stream.PublishGroup(d.ToProtobuf())

return &pb.Empty{}, err
}
Expand All @@ -50,6 +50,33 @@ func (s *grpcServer) RemoveGroup(ctx context.Context, e *pb.RemoveGroupRequest)
return &pb.Empty{}, err
}

func (s *grpcServer) UpdateGroup(ctx context.Context, e *pb.UpdateGroupRequest) (*pb.Empty, error) {
d := database.Groups{}
d.Name = e.GroupEntry.GroupName
d.Prefix = e.GroupEntry.GroupPrefix

var dbPerms []database.Permissions
for _, v := range e.GroupEntry.Permissions {
for _, p := range v.Permissions {
perm := database.Permissions{
ServerName: v.ServerName,
Permission: p,
}
dbPerms = append(dbPerms, perm)
}
}
d.Permissions = dbPerms

err := s.mysql.UpdateGroup(d)
if err != nil {
return &pb.Empty{}, err
}

stream.PublishGroup(d.ToProtobuf())

return &pb.Empty{}, err
}

func (s *grpcServer) AddPermission(ctx context.Context, e *pb.AddPermissionRequest) (*pb.Empty, error) {
if err := s.mysql.AddPermission(e.GroupName, e.Target, e.Permissions); err != nil {
return &pb.Empty{}, err
Expand Down
Loading

0 comments on commit bed4005

Please sign in to comment.