Skip to content

Commit

Permalink
feat(misc): grant/revoke user permission in Console (#379)
Browse files Browse the repository at this point in the history
  • Loading branch information
bouassaba authored Nov 14, 2024
1 parent f67c557 commit 1665063
Show file tree
Hide file tree
Showing 68 changed files with 1,363 additions and 1,226 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
.idea
.vscode
.DS_Store
2 changes: 1 addition & 1 deletion api/errorpkg/error_creators.go
Original file line number Diff line number Diff line change
Expand Up @@ -233,7 +233,7 @@ func NewCannotRemoveSoleOwnerOfGroupError(group model.Group) *ErrorResponse {
)
}

func NewGroupPermissionError(userID string, org model.Organization, permission string) *ErrorResponse {
func NewGroupPermissionError(userID string, org model.Group, permission string) *ErrorResponse {
return NewErrorResponse(
"missing_group_permission",
http.StatusForbidden,
Expand Down
22 changes: 20 additions & 2 deletions api/search/file_search.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,16 @@ type FileSearch struct {
snapshotRepo repo.SnapshotRepo
}

type fileEntity struct {
ID string `json:"id"`
Name string `json:"name"`
Text *string `json:"text,omitempty"`
}

func (f fileEntity) GetID() string {
return f.ID
}

func NewFileSearch() *FileSearch {
return &FileSearch{
index: infra.FileSearchIndex,
Expand All @@ -45,7 +55,7 @@ func (s *FileSearch) Index(files []model.File) (err error) {
}
var res []infra.SearchModel
for _, f := range files {
res = append(res, f)
res = append(res, s.mapEntity(f))
}
if err := s.search.Index(s.index, res); err != nil {
return err
Expand All @@ -62,7 +72,7 @@ func (s *FileSearch) Update(files []model.File) (err error) {
}
var res []infra.SearchModel
for _, f := range files {
res = append(res, f)
res = append(res, s.mapEntity(f))
}
if err := s.search.Update(s.index, res); err != nil {
return err
Expand Down Expand Up @@ -119,3 +129,11 @@ func (s *FileSearch) populateTextField(files []model.File) error {
}
return nil
}

func (s *FileSearch) mapEntity(file model.File) *fileEntity {
return &fileEntity{
ID: file.GetID(),
Name: file.GetName(),
Text: file.GetText(),
}
}
20 changes: 18 additions & 2 deletions api/search/group_search.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,15 @@ type GroupSearch struct {
groupRepo repo.GroupRepo
}

type groupEntity struct {
ID string `json:"id"`
Name string `json:"name"`
}

func (g groupEntity) GetID() string {
return g.ID
}

func NewGroupSearch() *GroupSearch {
return &GroupSearch{
index: infra.GroupSearchIndex,
Expand All @@ -38,7 +47,7 @@ func (s *GroupSearch) Index(groups []model.Group) error {
}
var res []infra.SearchModel
for _, g := range groups {
res = append(res, g)
res = append(res, s.mapEntity(g))
}
if err := s.search.Index(s.index, res); err != nil {
return err
Expand All @@ -52,7 +61,7 @@ func (s *GroupSearch) Update(groups []model.Group) error {
}
var res []infra.SearchModel
for _, g := range groups {
res = append(res, g)
res = append(res, s.mapEntity(g))
}
if err := s.search.Update(s.index, res); err != nil {
return err
Expand Down Expand Up @@ -90,3 +99,10 @@ func (s *GroupSearch) Query(query string, opts infra.QueryOptions) ([]model.Grou
}
return res, nil
}

func (s *GroupSearch) mapEntity(group model.Group) *groupEntity {
return &groupEntity{
ID: group.GetID(),
Name: group.GetName(),
}
}
20 changes: 18 additions & 2 deletions api/search/organization_search.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,15 @@ type OrganizationSearch struct {
orgRepo repo.OrganizationRepo
}

type organizationEntity struct {
ID string `json:"id"`
Name string `json:"name"`
}

func (o organizationEntity) GetID() string {
return o.ID
}

func NewOrganizationSearch() *OrganizationSearch {
return &OrganizationSearch{
index: infra.OrganizationSearchIndex,
Expand All @@ -38,7 +47,7 @@ func (s *OrganizationSearch) Index(orgs []model.Organization) error {
}
var res []infra.SearchModel
for _, o := range orgs {
res = append(res, o)
res = append(res, s.mapEntity(o))
}
if err := s.search.Index(s.index, res); err != nil {
return err
Expand All @@ -52,7 +61,7 @@ func (s *OrganizationSearch) Update(orgs []model.Organization) error {
}
var res []infra.SearchModel
for _, o := range orgs {
res = append(res, o)
res = append(res, s.mapEntity(o))
}
if err := s.search.Update(s.index, res); err != nil {
return err
Expand Down Expand Up @@ -90,3 +99,10 @@ func (s *OrganizationSearch) Query(query string, opts infra.QueryOptions) ([]mod
}
return res, nil
}

func (s *OrganizationSearch) mapEntity(org model.Organization) *organizationEntity {
return &organizationEntity{
ID: org.GetID(),
Name: org.GetName(),
}
}
24 changes: 20 additions & 4 deletions api/search/task_search.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,15 @@ type TaskSearch struct {
taskRepo repo.TaskRepo
}

type taskEntity struct {
ID string `json:"id"`
Name string `json:"name"`
}

func (t taskEntity) GetID() string {
return t.ID
}

func NewTaskSearch() *TaskSearch {
return &TaskSearch{
index: infra.TaskSearchIndex,
Expand All @@ -37,8 +46,8 @@ func (s *TaskSearch) Index(tasks []model.Task) error {
return nil
}
var res []infra.SearchModel
for _, o := range tasks {
res = append(res, o)
for _, t := range tasks {
res = append(res, s.mapEntity(t))
}
if err := s.search.Index(s.index, res); err != nil {
return err
Expand All @@ -51,8 +60,8 @@ func (s *TaskSearch) Update(orgs []model.Task) error {
return nil
}
var res []infra.SearchModel
for _, o := range orgs {
res = append(res, o)
for _, t := range orgs {
res = append(res, s.mapEntity(t))
}
if err := s.search.Update(s.index, res); err != nil {
return err
Expand Down Expand Up @@ -90,3 +99,10 @@ func (s *TaskSearch) Query(query string, opts infra.QueryOptions) ([]model.Task,
}
return res, nil
}

func (s *TaskSearch) mapEntity(task model.Task) *taskEntity {
return &taskEntity{
ID: task.GetID(),
Name: task.GetName(),
}
}
20 changes: 18 additions & 2 deletions api/search/workspace_search.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,15 @@ type WorkspaceSearch struct {
workspaceRepo repo.WorkspaceRepo
}

type workspaceEntity struct {
ID string `json:"id"`
Name string `json:"name"`
}

func (w workspaceEntity) GetID() string {
return w.ID
}

func NewWorkspaceSearch() *WorkspaceSearch {
return &WorkspaceSearch{
index: infra.WorkspaceSearchIndex,
Expand All @@ -38,7 +47,7 @@ func (s *WorkspaceSearch) Index(workspaces []model.Workspace) error {
}
var res []infra.SearchModel
for _, w := range workspaces {
res = append(res, w)
res = append(res, s.mapEntity(w))
}
if err := s.search.Index(s.index, res); err != nil {
return err
Expand All @@ -52,7 +61,7 @@ func (s *WorkspaceSearch) Update(workspaces []model.Workspace) error {
}
var res []infra.SearchModel
for _, w := range workspaces {
res = append(res, w)
res = append(res, s.mapEntity(w))
}
if err := s.search.Update(s.index, res); err != nil {
return err
Expand Down Expand Up @@ -90,3 +99,10 @@ func (s *WorkspaceSearch) Query(query string, opts infra.QueryOptions) ([]model.
}
return res, nil
}

func (s *WorkspaceSearch) mapEntity(workspace model.Workspace) *workspaceEntity {
return &workspaceEntity{
ID: workspace.GetID(),
Name: workspace.GetName(),
}
}
10 changes: 9 additions & 1 deletion api/service/group_service.go
Original file line number Diff line number Diff line change
Expand Up @@ -220,10 +220,18 @@ func (svc *GroupService) findAll(opts GroupListOptions, userID string) ([]model.
if err != nil {
return nil, err
}
groups, err := svc.groupSearch.Query(opts.Query, infra.QueryOptions{Limit: count})
hits, err := svc.groupSearch.Query(opts.Query, infra.QueryOptions{Limit: count})
if err != nil {
return nil, err
}
var groups []model.Group
for _, hit := range hits {
group, err := svc.groupCache.Get(hit.GetID())
if err != nil {
continue
}
groups = append(groups, group)
}
var filtered []model.Group
if opts.OrganizationID == "" {
filtered = groups
Expand Down
10 changes: 9 additions & 1 deletion api/service/organization_service.go
Original file line number Diff line number Diff line change
Expand Up @@ -194,10 +194,18 @@ func (svc *OrganizationService) findAll(opts OrganizationListOptions, userID str
if err != nil {
return nil, err
}
orgs, err := svc.orgSearch.Query(opts.Query, infra.QueryOptions{Limit: count})
hits, err := svc.orgSearch.Query(opts.Query, infra.QueryOptions{Limit: count})
if err != nil {
return nil, err
}
var orgs []model.Organization
for _, hit := range hits {
org, err := svc.orgCache.Get(hit.GetID())
if err != nil {
continue
}
orgs = append(orgs, org)
}
res, err = svc.doAuthorization(orgs, userID)
if err != nil {
return nil, err
Expand Down
10 changes: 9 additions & 1 deletion api/service/task_service.go
Original file line number Diff line number Diff line change
Expand Up @@ -240,10 +240,18 @@ func (svc *TaskService) findAll(opts TaskListOptions, userID string) ([]model.Ta
if err != nil {
return nil, err
}
tasks, err := svc.taskSearch.Query(opts.Query, infra.QueryOptions{Limit: count})
hits, err := svc.taskSearch.Query(opts.Query, infra.QueryOptions{Limit: count})
if err != nil {
return nil, err
}
var tasks []model.Task
for _, hit := range hits {
task, err := svc.taskCache.Get(hit.GetID())
if err != nil {
continue
}
tasks = append(tasks, task)
}
res, err = svc.doAuthorization(tasks, userID)
if err != nil {
return nil, err
Expand Down
10 changes: 9 additions & 1 deletion api/service/workspace_service.go
Original file line number Diff line number Diff line change
Expand Up @@ -334,10 +334,18 @@ func (svc *WorkspaceService) findAll(opts WorkspaceListOptions, userID string) (
if err != nil {
return nil, err
}
workspaces, err := svc.workspaceSearch.Query(opts.Query, infra.QueryOptions{Limit: count})
hits, err := svc.workspaceSearch.Query(opts.Query, infra.QueryOptions{Limit: count})
if err != nil {
return nil, err
}
var workspaces []model.Workspace
for _, hit := range hits {
workspace, err := svc.workspaceCache.Get(hit.GetID())
if err != nil {
continue
}
workspaces = append(workspaces, workspace)
}
res, err = svc.doAuthorization(workspaces, userID)
if err != nil {
return nil, err
Expand Down
2 changes: 1 addition & 1 deletion console/Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
# by the GNU Affero General Public License v3.0 only, included in the file
# licenses/AGPL.txt.

FROM python:3.12.5-alpine
FROM python:3.11.9-alpine

ARG USERNAME=voltaserve
ARG GROUPNAME=$USERNAME
Expand Down
3 changes: 1 addition & 2 deletions console/api/database/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@
fetch_organization_users,
fetch_organization_workspaces,
fetch_organization_groups,
update_organization,
fetch_organization_count,
)
from .snapshot import fetch_snapshot, fetch_snapshots
Expand All @@ -31,7 +30,7 @@
from .workspace import (
fetch_workspace,
fetch_workspaces,
update_workspace,
fetch_workspace_count,
)
from .overview import fetch_version
from .userpermission import grant_user_permission, revoke_user_permission
19 changes: 1 addition & 18 deletions console/api/database/group.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
# the Business Source License, use of this software will be governed
# by the GNU Affero General Public License v3.0 only, included in the file
# licenses/AGPL.txt.

from typing import Tuple, Iterable, Dict

from psycopg import DatabaseError
Expand All @@ -16,7 +17,6 @@
from .generic import exists


# --- FETCH --- #
def fetch_group(_id: str) -> Dict:
try:
with conn.cursor() as curs:
Expand Down Expand Up @@ -101,20 +101,3 @@ def fetch_groups(page=1, size=10) -> Tuple[Iterable[Dict], int]:

except DatabaseError as error:
raise error


# --- UPDATE --- #
def update_group(data: dict) -> None:
try:
with conn.cursor() as curs:
if not exists(curs=curs, _id=data["id"], tablename="group"):
raise NotFoundException(f"Group with id={data['id']} does not exist!")

curs.execute(parse_sql_update_query("group", data))
except DatabaseError as error:
raise error


# --- CREATE --- #

# --- DELETE --- #
Loading

0 comments on commit 1665063

Please sign in to comment.