UpdateRole updates an existing role.
(ctx context.Context, req *connect.Request[v1pb.UpdateRoleRequest])
| 121 | |
| 122 | // UpdateRole updates an existing role. |
| 123 | func (s *RoleService) UpdateRole(ctx context.Context, req *connect.Request[v1pb.UpdateRoleRequest]) (*connect.Response[v1pb.Role], error) { |
| 124 | workspaceID := common.GetWorkspaceIDFromContext(ctx) |
| 125 | if err := s.licenseService.IsFeatureEnabled(ctx, workspaceID, v1pb.PlanFeature_FEATURE_CUSTOM_ROLES); err != nil { |
| 126 | return nil, connect.NewError(connect.CodePermissionDenied, err) |
| 127 | } |
| 128 | if req.Msg.UpdateMask == nil { |
| 129 | return nil, connect.NewError(connect.CodeInvalidArgument, errors.New("update_mask must be set")) |
| 130 | } |
| 131 | roleID, err := common.GetRoleID(req.Msg.Role.Name) |
| 132 | if err != nil { |
| 133 | return nil, connect.NewError(connect.CodeInvalidArgument, err) |
| 134 | } |
| 135 | if predefinedRole := s.getBuildinRole(roleID); predefinedRole != nil { |
| 136 | return nil, connect.NewError(connect.CodeInvalidArgument, errors.Errorf("cannot change the build-in role %s", req.Msg.Role.Name)) |
| 137 | } |
| 138 | role, err := s.store.GetRole(ctx, &store.FindRoleMessage{Workspace: workspaceID, ResourceID: &roleID}) |
| 139 | if err != nil { |
| 140 | return nil, connect.NewError(connect.CodeInternal, errors.Wrap(err, "failed to get role")) |
| 141 | } |
| 142 | if role == nil { |
| 143 | if req.Msg.AllowMissing { |
| 144 | return s.CreateRole(ctx, connect.NewRequest(&v1pb.CreateRoleRequest{ |
| 145 | Role: req.Msg.Role, |
| 146 | RoleId: roleID, |
| 147 | })) |
| 148 | } |
| 149 | return nil, connect.NewError(connect.CodeNotFound, errors.Errorf("role not found: %s", roleID)) |
| 150 | } |
| 151 | patch := &store.UpdateRoleMessage{ |
| 152 | ResourceID: roleID, |
| 153 | Workspace: workspaceID, |
| 154 | } |
| 155 | for _, path := range req.Msg.UpdateMask.Paths { |
| 156 | switch path { |
| 157 | case "title": |
| 158 | patch.Name = &req.Msg.Role.Title |
| 159 | case "description": |
| 160 | patch.Description = &req.Msg.Role.Description |
| 161 | case "permissions": |
| 162 | permissions := make(map[permission.Permission]bool) |
| 163 | for _, v := range req.Msg.GetRole().GetPermissions() { |
| 164 | permissions[permission.Permission(v)] = true |
| 165 | } |
| 166 | patch.Permissions = &permissions |
| 167 | if ok := permission.Exists(req.Msg.Role.Permissions...); !ok { |
| 168 | invalidPerms := []string{} |
| 169 | for _, p := range req.Msg.Role.Permissions { |
| 170 | if !permission.Exist(p) { |
| 171 | invalidPerms = append(invalidPerms, p) |
| 172 | } |
| 173 | } |
| 174 | return nil, connect.NewError(connect.CodeInvalidArgument, errors.Errorf("invalid permissions: %v", invalidPerms)) |
| 175 | } |
| 176 | default: |
| 177 | return nil, connect.NewError(connect.CodeInvalidArgument, errors.Errorf("invalid update mask path: %s", path)) |
| 178 | } |
| 179 | } |
| 180 |
nothing calls this directly
no test coverage detected