ProjectRoleController.cs 4.04 KB
Newer Older
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
using Coscine.Api.Project.Models;
using Coscine.Api.Project.ReturnObjects;
using Coscine.ApiCommons;
using Coscine.ApiCommons.Exceptions;
using Coscine.ApiCommons.Factories;
using Coscine.Database.Model;
using Microsoft.AspNetCore.Mvc;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Coscine.Api.Project.Controllers
{
    public class ProjectRoleController : Controller
    {
        private readonly Authenticator _authenticator;
        private readonly ProjectRoleModel _projectRoleModel;

        public ProjectRoleController()
        {
            _authenticator = new Authenticator(this, Program.Configuration);
            _projectRoleModel = new ProjectRoleModel();
        }

        [Route("[controller]/{projectId}")]
        public IActionResult Index(string projectId)
        {
            return Ok(_authenticator.ValidateAndExecute((user) =>
            {
                UserModel userModel = new UserModel();
                RoleModel roleModel = new RoleModel();
                ProjectModel projectModel = new ProjectModel();
                Guid.TryParse(projectId, out Guid projectIdGuid);
                if (projectModel.OwnsProject(user, projectModel.GetById(projectIdGuid)))
                {
                    return _projectRoleModel.GetAllWhere((projectRole) =>
39
                        (projectRole.ProjectId == projectIdGuid)
40
41
42
43
44
45
46
47
48
49
50
51
                    ).Select((projectRole) =>
                    {
                        User userInst = projectRole.User;
                        if (userInst == null)
                        {
                            userInst = userModel.GetById(projectRole.UserId);
                        }
                        Role role = projectRole.Role;
                        if (role == null)
                        {
                            role = roleModel.GetById(projectRole.RoleId);
                        }
52
                        return new ProjectRoleObject(projectRole.ProjectId, new UserObject(userInst.Id, userInst.DisplayName, userInst.EmailAddress), new RoleObject(role.Id, role.DisplayName));
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
                    });
                }
                else
                {
                    throw new UnauthorizedAccessException("User is not allowed to list all users to the given project!");
                }
            }));
        }

        [HttpPost("[controller]")]
        public IActionResult Set()
        {
            return Ok(_authenticator.ValidateAndExecute((user) =>
            {
                ProjectRoleObject projectRoleObject = ObjectFactory<ProjectRoleObject>.DeserializeFromStream(Request.Body);
                ProjectModel projectModel = new ProjectModel();
                if (projectModel.OwnsProject(user, projectModel.GetById(projectRoleObject.ProjectId)))
                {
                    return _projectRoleModel.SetFromObject(projectRoleObject);
                }
                else
                {
                    throw new NotAuthorizedException("The user is not authorized to store a project role to the given project!");
                }
            }));
        }

80
81
        [HttpDelete("[controller]/project/{projectId}/user/{userId}/role/{roleId}")]
        public IActionResult Delete(Guid projectId, Guid userId, Guid roleId)
82
83
84
85
        {
            return Ok(_authenticator.ValidateAndExecute((user) =>
            {
                ProjectModel projectModel = new ProjectModel();
86
                if (projectModel.OwnsProject(user, projectModel.GetById(projectId)))
87
88
                {
                    return _projectRoleModel.Delete(_projectRoleModel.GetWhere((projectRole) =>
89
90
91
                            projectRole.ProjectId == projectId
                            && projectRole.UserId == userId
                            && projectRole.RoleId == roleId));
92
93
94
95
96
97
98
99
100
                }
                else
                {
                    throw new NotAuthorizedException("The user is not authorized to delete a project role for the given project!");
                }
            }));
        }
    }
}