Commit 7c839772 authored by Marcel Nellesen's avatar Marcel Nellesen
Browse files

New: Added function to leave a project (coscine/issues#982)

parent e7dceeb9
......@@ -91,8 +91,9 @@ namespace Coscine.Api.Project.Controllers
return Json(_projectRoleModel.GetAllWhere((projectRole) =>
(projectRole.UserId == user.Id &&
projectRole.ProjectId == projectIdGuid)
).Select((projectRole) => {
if(projectRole.Role == null)
).Select((projectRole) =>
{
if (projectRole.Role == null)
{
projectRole.Role = roleModel.GetById(projectRole.RoleId);
}
......@@ -137,12 +138,12 @@ namespace Coscine.Api.Project.Controllers
{
var projectModel = new ProjectModel();
var user = _authenticator.GetUser();
var project = projectModel.GetById(projectId);
if (projectModel.HasAccess(user, projectModel.GetById(projectId), UserRoles.Owner))
if (projectModel.HasAccess(user, project, UserRoles.Owner))
{
_projectRoleModel.CheckIfLastOwnerWillBeRemoved(roleId, projectId);
var project = projectModel.GetById(projectId);
var userModel = new UserModel();
var userToRemove = userModel.GetById(userId);
......@@ -164,6 +165,36 @@ namespace Coscine.Api.Project.Controllers
return Unauthorized("The user is not authorized to delete a project role for the given project!");
}
}
[HttpDelete("[controller]/project/{projectId}/user")]
public IActionResult Delete(Guid projectId)
{
var projectModel = new ProjectModel();
var user = _authenticator.GetUser();
var project = projectModel.GetById(projectId);
var roleId = _projectRoleModel.GetGetUserRoleForProject(projectId, user.Id);
if (projectModel.HasAccess(user, project, UserRoles.Owner, UserRoles.Member))
{
_projectRoleModel.CheckIfLastOwnerWillBeRemoved((Guid)roleId, projectId);
_emitter.EmitUserRemove(new UserEventArgs(this._configuration)
{
Project = project,
User = user
});
LogAnalytics("Leave Project", user, null, project.Id.ToString());
return Json(_projectRoleModel.Delete(_projectRoleModel.GetWhere((projectRole) =>
projectRole.ProjectId == projectId
&& projectRole.UserId == user.Id
&& projectRole.RoleId == roleId)));
}
else
{
return Unauthorized("The user is not authorized to access the given project!");
}
}
private void LogAnalytics(string operation,
User user,
IEnumerable<ProjectRoleObject> users = null,
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment