Skip to content
GitLab
Menu
Projects
Groups
Snippets
/
Help
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Sign in
Toggle navigation
Menu
Open sidebar
Coscine
backend
apis
Project
Commits
31e8310c
Commit
31e8310c
authored
Sep 02, 2019
by
Benedikt Heinrichs
Browse files
Fix: Last Owner is not able to be deleted (coscine/issues#251)
parent
c44d1dc2
Changes
2
Pipelines
1
Hide whitespace changes
Inline
Side-by-side
src/Project/Controllers/ProjectRoleController.cs
View file @
31e8310c
...
...
@@ -85,6 +85,8 @@ namespace Coscine.Api.Project.Controllers
ProjectModel
projectModel
=
new
ProjectModel
();
if
(
projectModel
.
OwnsProject
(
user
,
projectModel
.
GetById
(
projectId
)))
{
_projectRoleModel
.
CheckIfLastOwnerWillBeRemoved
(
roleId
,
projectId
);
return
_projectRoleModel
.
Delete
(
_projectRoleModel
.
GetWhere
((
projectRole
)
=>
projectRole
.
ProjectId
==
projectId
&&
projectRole
.
UserId
==
userId
...
...
src/Project/Models/ProjectRoleModel.cs
View file @
31e8310c
using
Coscine.Api.Project.ReturnObjects
;
using
Coscine.ApiCommons.Exceptions
;
using
Coscine.ApiCommons.Models
;
using
Coscine.Database.Model
;
using
LinqToDB
;
...
...
@@ -18,11 +19,13 @@ namespace Coscine.Api.Project.Models
public
ProjectRole
SetFromObject
(
ProjectRoleObject
projectRoleObject
)
{
// Remove existing roles if they exist
var
existingRoles
=
GetAllWhere
((
dbProjectRole
)
=>
dbProjectRole
.
ProjectId
==
projectRoleObject
.
ProjectId
&&
dbProjectRole
.
UserId
==
projectRoleObject
.
User
.
Id
);
if
(
existingRoles
.
Count
()
>
0
)
var
existingRoles
=
GetAllWhere
((
dbProjectRole
)
=>
dbProjectRole
.
ProjectId
==
projectRoleObject
.
ProjectId
&&
dbProjectRole
.
UserId
==
projectRoleObject
.
User
.
Id
);
if
(
existingRoles
.
Count
()
>
0
)
{
foreach
(
var
role
in
existingRoles
)
{
CheckIfLastOwnerWillBeRemoved
(
role
.
RoleId
,
projectRoleObject
.
ProjectId
);
Delete
(
role
);
}
}
...
...
@@ -36,6 +39,24 @@ namespace Coscine.Api.Project.Models
return
projectRole
;
}
public
void
CheckIfLastOwnerWillBeRemoved
(
Guid
roleId
,
Guid
projectId
)
{
RoleModel
roleModel
=
new
RoleModel
();
var
ownerRole
=
roleModel
.
GetOwnerRole
();
if
(
roleId
==
ownerRole
.
Id
)
{
var
moreThanOneOwnerExists
=
GetAllWhere
((
projectRole
)
=>
projectRole
.
ProjectId
==
projectId
&&
projectRole
.
RoleId
==
ownerRole
.
Id
).
Count
()
>
1
;
if
(!
moreThanOneOwnerExists
)
{
throw
new
NotAuthorizedException
(
"The last owner cannot be removed!"
);
}
}
}
public
override
Expression
<
Func
<
ProjectRole
,
Guid
>>
GetIdFromObject
()
{
return
databaseObject
=>
databaseObject
.
RelationId
;
...
...
Ghost User
@ghost
mentioned in commit
522d7ce9
·
Sep 03, 2019
mentioned in commit
522d7ce9
mentioned in commit 522d7ce9a2bd27e3b7f41dea8f9a2fb78e9e09d3
Toggle commit list
Write
Preview
Supports
Markdown
0%
Try again
or
attach a new file
.
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment