canWriteWorksheet check if the principal can write the worksheet. worksheet is writable when the user has bb.worksheets.manage permission on the workspace, or. PRIVATE: the creator. PROJECT_WRITE: all members with bb.projects.get permission in the project.
(ctx context.Context, worksheet *store.WorkSheetMessage)
| 406 | // PRIVATE: the creator. |
| 407 | // PROJECT_WRITE: all members with bb.projects.get permission in the project. |
| 408 | func (s *WorksheetService) canWriteWorksheet(ctx context.Context, worksheet *store.WorkSheetMessage) (bool, error) { |
| 409 | user, ok := GetUserFromContext(ctx) |
| 410 | if !ok { |
| 411 | return false, connect.NewError(connect.CodeInternal, errors.Errorf("user not found")) |
| 412 | } |
| 413 | |
| 414 | // Worksheet creator and workspace "bb.worksheets.manage" can always write. |
| 415 | if worksheet.Creator == user.Email { |
| 416 | return true, nil |
| 417 | } |
| 418 | ok, err := s.iamManager.CheckPermission(ctx, permission.WorksheetsManage, user, common.GetWorkspaceIDFromContext(ctx)) |
| 419 | if err != nil { |
| 420 | return false, connect.NewError(connect.CodeInternal, errors.Errorf("failed to check permission with error: %v", err.Error())) |
| 421 | } |
| 422 | if ok { |
| 423 | return true, nil |
| 424 | } |
| 425 | |
| 426 | switch worksheet.Visibility { |
| 427 | case store.PrivateWorkSheet: |
| 428 | return false, nil |
| 429 | case store.ProjectReadWorkSheet: |
| 430 | // For READ visibility, check the "bb.worksheets.manage" permission in the project. |
| 431 | return s.checkWorksheetPermission(ctx, worksheet.ProjectID, user, permission.WorksheetsManage) |
| 432 | case store.ProjectWriteWorkSheet: |
| 433 | // For READ visibility, needs "bb.worksheets.get" permission in the project. |
| 434 | return s.checkWorksheetPermission(ctx, worksheet.ProjectID, user, permission.WorksheetsGet) |
| 435 | default: |
| 436 | return false, nil |
| 437 | } |
| 438 | } |
| 439 | |
| 440 | // canReadWorksheet check if the principal can read the worksheet. |
| 441 | // worksheet is readable when the user has bb.worksheets.get permission on the workspace, or. |
no test coverage detected