This is an automated email from the ASF dual-hosted git repository.

DImuthuUpe pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/airavata.git


The following commit(s) were added to refs/heads/master by this push:
     new 0584e4e1bb Dashboard support to make users admin (#762)
0584e4e1bb is described below

commit 0584e4e1bbb533a3eed74ae35967cff854dbc3e2
Author: Dimuthu Wannipurage <[email protected]>
AuthorDate: Tue Nov 10 23:59:30 2026 -0500

    Dashboard support to make users admin (#762)
    
    * Initial jupyter ui integration
    
    * Implemented the file browser for the notebook viewer
    
    * Integrating the support to connect to remote jupyter server using linkspan
    
    * Added CILogon auth to the dashboard
    
    * Auto create user with inactive status for cilogon auth
    
    * Add UI to share cluster configs with users and groups
    
    * Showing group management ui
    
    * Updated template changes
    
    * Refactoring notebook viewer ux
    
    * UI support to make users admin
---
 api/iam/controller/user.go       | 15 ++++++++++++++
 api/iam/dto/user.go              |  5 +++++
 api/iam/repository/user.go       | 11 +++++++++++
 api/iam/service/user.go          | 42 ++++++++++++++++++++++++++++++++++++++++
 dashboard/src/app/users/page.tsx | 30 +++++++++++++++++++++++++++-
 5 files changed, 102 insertions(+), 1 deletion(-)

diff --git a/api/iam/controller/user.go b/api/iam/controller/user.go
index 6a6c8df474..a438961207 100644
--- a/api/iam/controller/user.go
+++ b/api/iam/controller/user.go
@@ -46,6 +46,7 @@ func (h *UserController) Register(mux *http.ServeMux) {
        mux.HandleFunc("GET "+auth.CurrentUserPath, h.me)
        mux.HandleFunc("GET /api/v1/users/{userId}", h.get)
        mux.HandleFunc("POST /api/v1/users/{userId}/status", h.setStatus)
+       mux.HandleFunc("POST /api/v1/users/{userId}/admin", h.setAdmin)
        mux.HandleFunc("POST /api/v1/users/{userId}", h.update)
 }
 
@@ -122,3 +123,17 @@ func (h *UserController) setStatus(w http.ResponseWriter, 
r *http.Request) {
        }
        httpx.WriteJSON(w, http.StatusOK, user)
 }
+
+func (h *UserController) setAdmin(w http.ResponseWriter, r *http.Request) {
+       var req dto.UserAdminUpdate
+       if err := httpx.Bind(r, &req); err != nil {
+               httpx.WriteError(w, r, err)
+               return
+       }
+       user, err := h.svc.SetAdmin(r.Context(), r.PathValue("userId"), &req)
+       if err != nil {
+               httpx.WriteError(w, r, err)
+               return
+       }
+       httpx.WriteJSON(w, http.StatusOK, user)
+}
diff --git a/api/iam/dto/user.go b/api/iam/dto/user.go
index c709fbcc4a..4da488213f 100644
--- a/api/iam/dto/user.go
+++ b/api/iam/dto/user.go
@@ -128,3 +128,8 @@ func (r *UserStatusUpdate) Validate() []httpx.FieldError {
        }
        return c.Fields()
 }
+
+// UserAdminUpdate grants or withdraws the ADMIN role.
+type UserAdminUpdate struct {
+       Admin bool `json:"admin"`
+}
diff --git a/api/iam/repository/user.go b/api/iam/repository/user.go
index 171921470b..fb5f227208 100644
--- a/api/iam/repository/user.go
+++ b/api/iam/repository/user.go
@@ -66,3 +66,14 @@ func (r *UserRepository) FindByID(ctx context.Context, id 
string) (*model.User,
 func (r *UserRepository) Save(ctx context.Context, u *model.User) error {
        return r.db.WithContext(ctx).Save(u).Error
 }
+
+// AddRole grants role to the user. Granting a role already held is a no-op.
+func (r *UserRepository) AddRole(ctx context.Context, userID string, role 
model.Role) error {
+       ur := model.UserRole{UserID: userID, Role: role}
+       return r.db.WithContext(ctx).Where(ur).FirstOrCreate(&ur).Error
+}
+
+// RemoveRole withdraws role from the user. Withdrawing a role not held is a 
no-op.
+func (r *UserRepository) RemoveRole(ctx context.Context, userID string, role 
model.Role) error {
+       return r.db.WithContext(ctx).Delete(&model.UserRole{}, "user_id = ? AND 
role = ?", userID, role).Error
+}
diff --git a/api/iam/service/user.go b/api/iam/service/user.go
index d71c59b175..9d6f326284 100644
--- a/api/iam/service/user.go
+++ b/api/iam/service/user.go
@@ -186,6 +186,48 @@ func (s *UserService) SetStatus(ctx context.Context, 
userID string, req *dto.Use
        return &out, nil
 }
 
+// SetAdmin grants or withdraws the ADMIN role. Super Admin only.
+//
+// A Super Admin cannot change their own role, and a Super Admin's standing is 
not
+// something this call touches: SUPER_ADMIN accounts are rejected. Service 
accounts
+// never hold administrator rights.
+func (s *UserService) SetAdmin(ctx context.Context, userID string, req 
*dto.UserAdminUpdate) (*dto.UserAccountResponse, error) {
+       principal, err := auth.RequireSuperAdmin(ctx)
+       if err != nil {
+               return nil, err
+       }
+       if principal.Name == userID {
+               return nil, httpx.Forbidden("You cannot change your own role")
+       }
+
+       user, err := s.repo.FindByIDWithRoles(ctx, userID)
+       if err != nil {
+               return nil, notFoundAs(err, "User not found with ID: %s", 
userID)
+       }
+       if user.IsServiceAccount() {
+               return nil, httpx.Invalid([]httpx.FieldError{{Field: "userId",
+                       Message: "Service accounts cannot be administrators"}})
+       }
+       if user.HasRole(model.RoleSuperAdmin) {
+               return nil, httpx.Forbidden("A Super Admin's role cannot be 
changed here")
+       }
+
+       if req.Admin {
+               err = s.repo.AddRole(ctx, userID, model.RoleAdmin)
+       } else {
+               err = s.repo.RemoveRole(ctx, userID, model.RoleAdmin)
+       }
+       if err != nil {
+               return nil, err
+       }
+       user, err = s.repo.FindByIDWithRoles(ctx, userID)
+       if err != nil {
+               return nil, err
+       }
+       out := dto.ToUserAccountResponse(user)
+       return &out, nil
+}
+
 // Update changes a user's profile. A Super Admin may update anyone; everyone 
else may
 // update only themselves.
 //
diff --git a/dashboard/src/app/users/page.tsx b/dashboard/src/app/users/page.tsx
index 621f0967bc..83b8fefe85 100644
--- a/dashboard/src/app/users/page.tsx
+++ b/dashboard/src/app/users/page.tsx
@@ -49,6 +49,16 @@ export default function UsersPage() {
     }
   };
 
+  const setAdmin = async (u: UserAccount, admin: boolean) => {
+    setError(null);
+    try {
+      await api.post(`/api/v1/users/${encodeURIComponent(u.userId)}/admin`, { 
admin });
+      await users.reload();
+    } catch (e) {
+      setError(errorText(e));
+    }
+  };
+
   if (!isAdmin) {
     return (
       <div className="stack">
@@ -143,7 +153,15 @@ export default function UsersPage() {
             {
               header: "",
               className: "actions",
-              cell: (u) => <StatusActions user={u} self={u.userId === 
me?.userId} canTouchAdmins={isSuperAdmin} onSet={setStatus} />,
+              cell: (u) => (
+                <StatusActions
+                  user={u}
+                  self={u.userId === me?.userId}
+                  canTouchAdmins={isSuperAdmin}
+                  onSet={setStatus}
+                  onSetAdmin={setAdmin}
+                />
+              ),
             },
           ]}
         />
@@ -157,11 +175,13 @@ function StatusActions({
   self,
   canTouchAdmins,
   onSet,
+  onSetAdmin,
 }: {
   user: UserAccount;
   self: boolean;
   canTouchAdmins: boolean;
   onSet: (u: UserAccount, status: UserStatus) => Promise<void>;
+  onSetAdmin: (u: UserAccount, admin: boolean) => Promise<void>;
 }) {
   const [busy, setBusy] = useState(false);
   const status = user.status ?? "ACTIVE";
@@ -186,6 +206,14 @@ function StatusActions({
 
   return (
     <div className="row" style={{ justifyContent: "flex-end" }}>
+      {canTouchAdmins && !user.roles.includes("SUPER_ADMIN") && (
+        <ConfirmButton
+          confirmText={user.roles.includes("ADMIN") ? "Remove admin?" : "Make 
admin?"}
+          onConfirm={() => onSetAdmin(user, !user.roles.includes("ADMIN"))}
+        >
+          {user.roles.includes("ADMIN") ? "Remove admin" : "Make admin"}
+        </ConfirmButton>
+      )}
       {status !== "ACTIVE" && (
         <button className="small primary" disabled={busy} onClick={activate}>
           {busy ? "Working…" : "Activate"}

Reply via email to