ProjectController.cs 4.6 KB
Newer Older
1
2
3
4
using Coscine.Action;
using Coscine.Action.EventArgs;
using Coscine.Action.Implementations.Project;
using Coscine.Api.Project.Exceptions;
5
6
7
8
using Coscine.Api.Project.Factories;
using Coscine.Api.Project.Models;
using Coscine.Api.Project.ReturnObjects;
using Microsoft.AspNetCore.Mvc;
9
using Microsoft.SharePoint;
10
using System;
11
using System.Collections.Generic;
12
using System.Linq;
13
using System.Management;
Benedikt Heinrichs's avatar
Benedikt Heinrichs committed
14
15
16
17
18
19

namespace Coscine.Api.Project.Controllers
{
    public class ProjectController : Controller
    {
        private readonly Authenticator _authenticator;
20
        private readonly List<IProjectAction> projectActions;
Benedikt Heinrichs's avatar
Benedikt Heinrichs committed
21
22
23
24

        public ProjectController()
        {
            _authenticator = new Authenticator(this);
25
26
27
28
29
            projectActions = new List<IProjectAction>()
            {
                new PIDAction(),
                new SharePointSiteAction()
            };
Benedikt Heinrichs's avatar
Benedikt Heinrichs committed
30
31
32
33
34
        }

        [Route("[controller]")]
        public IActionResult Index()
        {
35
36
37
            return Ok(_authenticator.ValidateAndExecute((user) =>
            {
                ProjectModel projectModel = new ProjectModel();
38
39
                return projectModel.GetAllWhere((project) =>                
                    (from projectRole in project.ProjectRolesProjectIdIds
40
41
                            where projectRole.User == user
                            && projectRole.Role.DisplayName == "Owner"
42
43
                            select projectRole).Any()
                ).Select((project) =>
44
                {
45
                    return new ProjectObject(project.Id, project.Description, project.DisplayName, project.Organization, project.StartDate, project.EndDate, project.Keywords);
46
47
                });
            }));
Benedikt Heinrichs's avatar
Benedikt Heinrichs committed
48
49
50
51
52
        }

        [HttpGet("[controller]/{id}")]
        public IActionResult Get(string id)
        {
53
54
55
56
            return Ok(_authenticator.ValidateAndExecute((user) =>
            {
                ProjectModel projectModel = new ProjectModel();
                var project = projectModel.GetById(Guid.Parse(id));
57
                return new ProjectObject(project.Id, project.Description, project.DisplayName, project.Organization, project.StartDate, project.EndDate, project.Keywords);
58
            }));
Benedikt Heinrichs's avatar
Benedikt Heinrichs committed
59
60
61
62
63
        }

        [HttpPost("[controller]/{id}")]
        public IActionResult Update(string id)
        {
64
65
66
67
68
69
70
            return Ok(_authenticator.ValidateAndExecute((user) =>
            {
                ProjectObject projectObject = ObjectFactory<ProjectObject>.DeserializeFromStream(Request.Body);
                ProjectModel projectModel = new ProjectModel();
                var project = projectModel.GetById(Guid.Parse(id));
                if(projectModel.OwnsProject(user, project))
                {
71
                    return projectModel.UpdateByObject(project, projectObject);
72
73
74
                }
                else
                {
75
                    throw new NotAuthorizedException("The user is not authorized to perform an update on the selected project!");
76
77
                }
            }));
Benedikt Heinrichs's avatar
Benedikt Heinrichs committed
78
79
        }

80
        [HttpPost("[controller]")]
Benedikt Heinrichs's avatar
Benedikt Heinrichs committed
81
82
        public IActionResult Store()
        {
83
            return base.Ok(_authenticator.ValidateAndExecute((user) =>
84
85
86
87
            {
                ProjectObject projectObject = ObjectFactory<ProjectObject>.DeserializeFromStream(Request.Body);
                ProjectModel projectModel = new ProjectModel();
                var project = projectModel.StoreFromObject(projectObject, user);
88
89
90

                FireEvents((projectAction, projectEventArgs) => projectAction.OnProjectCreate(project, projectEventArgs));

91
                return new ProjectObject(project.Id, project.Description, project.DisplayName, project.Organization, project.StartDate, project.EndDate, project.Keywords);
92
            }));
Benedikt Heinrichs's avatar
Benedikt Heinrichs committed
93
        }
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116

        private void FireEvents(Action<IProjectAction, ProjectEventArgs> eventAction)
        {
            var sharePointSite = Request.Headers["Referer"];

            SPUserToken systemAccount = SPUserToken.SystemAccount;
            using (SPSite site = new SPSite(sharePointSite, systemAccount))
            {
                using (SPWeb web = site.OpenWeb())
                {
                    SPContext context = SPContext.GetContext(web);
                    object[] args = new object[]
                    {
                        context
                    };
                    ProjectEventArgs projectEventArgs = new ProjectEventArgs(Program.Configuration, args);
                    foreach (var projectAction in projectActions)
                    {
                        eventAction(projectAction, projectEventArgs);
                    }
                }
            }
        }
Benedikt Heinrichs's avatar
Benedikt Heinrichs committed
117
118
    }
}