Aufgrund einer Störung des s3 Storage, könnten in nächster Zeit folgende GitLab Funktionen nicht zur Verfügung stehen: Container Registry, Job Artifacs,. Wir bitten um Verständnis. Es wird mit Hochdruck an der Behebung des Problems gearbeitet. Weitere Informationen zur Störung des Object Storage finden Sie hier: https://maintenance.itc.rwth-aachen.de/ticket/status/messages/59-object-storage-pilot

Commit d6a3c2ca authored by Benedikt Heinrichs's avatar Benedikt Heinrichs
Browse files

Merge branch 'Topic/180-SubPages' into 'Product/168-BasicStructure'

Topic/180 sub pages

See merge request coscine/api/project!5
parents bd1ed418 40f96fc8
......@@ -55,6 +55,9 @@
<Reference Include="Consul, Version=0.7.2.6, Culture=neutral, PublicKeyToken=20a6ad9a81df1d95, processorArchitecture=MSIL">
<HintPath>..\packages\Consul.0.7.2.6\lib\net45\Consul.dll</HintPath>
</Reference>
<Reference Include="Coscine.Action, Version=1.1.1.0, Culture=neutral, processorArchitecture=MSIL">
<HintPath>..\packages\Coscine.Action.1.1.1\lib\net461\Coscine.Action.dll</HintPath>
</Reference>
<Reference Include="Coscine.ApiCommons, Version=1.0.0.0, Culture=neutral, processorArchitecture=MSIL">
<HintPath>..\packages\Coscine.ApiCommons.1.0.0\lib\net461\Coscine.ApiCommons.dll</HintPath>
</Reference>
......@@ -64,6 +67,12 @@
<Reference Include="Coscine.Database, Version=1.4.0.0, Culture=neutral, PublicKeyToken=767d77427707b70a, processorArchitecture=MSIL">
<HintPath>..\packages\Coscine.Database.1.4.0\lib\net461\Coscine.Database.dll</HintPath>
</Reference>
<Reference Include="Coscine.ProxyApi, Version=1.2.0.0, Culture=neutral, processorArchitecture=MSIL">
<HintPath>..\packages\Coscine.ProxyApi.1.2.0\lib\net461\Coscine.ProxyApi.dll</HintPath>
</Reference>
<Reference Include="Coscine.SharePoint.Webparts.Vue, Version=1.3.0.0, Culture=neutral, PublicKeyToken=0fe8d3e516df6d98, processorArchitecture=MSIL">
<HintPath>..\packages\Coscine.SharePoint.Webparts.Vue.1.3.0\lib\net461\Coscine.SharePoint.Webparts.Vue.dll</HintPath>
</Reference>
<Reference Include="EntityFramework, Version=6.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089, processorArchitecture=MSIL">
<HintPath>..\packages\EntityFramework.6.2.0\lib\net45\EntityFramework.dll</HintPath>
</Reference>
......
......@@ -64,6 +64,7 @@ namespace Coscine.Api.Project.Tests
}
[Test]
public void TestControllerUpdate()
{
......@@ -138,6 +139,69 @@ namespace Coscine.Api.Project.Tests
projectModel.Delete(project);
}
[Test]
public void TestControllerStoreWithSubProject()
{
ProjectObject newProjectObject = new ProjectObject(Guid.NewGuid(), "NewProject", "NewDisplayName", "NewOrganisation", DateTime.Now, DateTime.Now.AddYears(1), "test2;test3");
var stream = ObjectFactory<ProjectObject>.SerializeToStream(newProjectObject);
FakeControllerContext(Users[0], stream);
var actionResult = Controller.Store();
OkObjectResult okObjectResult = (OkObjectResult)actionResult;
ProjectObject createdProjectObject = (ProjectObject)okObjectResult.Value;
ProjectObject newSubProjectObject = new ProjectObject(Guid.NewGuid(), "NewSubProject", "NewDisplayNameSub", "NewOrganisation", DateTime.Now, DateTime.Now.AddYears(1), "test2;test3", createdProjectObject.Id);
var subStream = ObjectFactory<ProjectObject>.SerializeToStream(newSubProjectObject);
FakeControllerContext(Users[0], subStream);
var subActionResult = Controller.Store();
OkObjectResult okSubObjectResult = (OkObjectResult)subActionResult;
ProjectObject createdSubProjectObject = (ProjectObject)okSubObjectResult.Value;
SubProjectModel subProjectModel = new SubProjectModel();
var subProjects = subProjectModel.GetAllWhere((x) => x.ProjectId == createdProjectObject.Id);
foreach(var subProject in subProjects)
{
Assert.IsTrue(subProject.SubProjectId == createdSubProjectObject.Id);
Assert.IsTrue(subProject.ProjectId == createdProjectObject.Id);
}
// Cleanup
stream.Close();
subStream.Close();
ProjectModel projectModel = new ProjectModel();
var project = projectModel.GetById(createdProjectObject.Id);
SubProjectModel subProjectModel1 = new SubProjectModel();
var subProjects1 = subProjectModel1.GetAllWhere((x) => x.ProjectId == project.Id);
foreach (var subProject in subProjects1)
{
subProjectModel1.Delete(subProject);
}
ProjectRoleModel projectRoleModel = new ProjectRoleModel();
var projectRoles = projectRoleModel.GetAllWhere((x) => x.ProjectId == project.Id);
foreach (var projectRole in projectRoles)
{
projectRoleModel.Delete(projectRole);
}
projectModel.Delete(project);
ProjectModel projectModel2 = new ProjectModel();
var project2 = projectModel2.GetById(createdSubProjectObject.Id);
ProjectRoleModel projectRoleModel2 = new ProjectRoleModel();
var projectRoles2 = projectRoleModel2.GetAllWhere((x) => x.ProjectId == project2.Id);
foreach (var projectRole in projectRoles2)
{
projectRoleModel2.Delete(projectRole);
}
projectModel2.Delete(project2);
}
[Test]
public void CountTest()
{
......
......@@ -4,9 +4,12 @@
<package id="AutoMapper.Extensions.Microsoft.DependencyInjection" version="6.0.0" targetFramework="net472" />
<package id="Castle.Core" version="4.4.0" targetFramework="net472" />
<package id="Consul" version="0.7.2.6" targetFramework="net472" />
<package id="Coscine.Action" version="1.1.1" targetFramework="net472" />
<package id="Coscine.ApiCommons" version="1.0.0" targetFramework="net472" />
<package id="Coscine.Configuration" version="1.4.0" targetFramework="net472" />
<package id="Coscine.Database" version="1.4.0" targetFramework="net472" />
<package id="Coscine.ProxyApi" version="1.2.0" targetFramework="net472" />
<package id="Coscine.SharePoint.Webparts.Vue" version="1.3.0" targetFramework="net472" />
<package id="EntityFramework" version="6.2.0" targetFramework="net472" />
<package id="linq2db" version="2.6.4" targetFramework="net472" />
<package id="linq2db.SqlServer" version="2.6.4" targetFramework="net472" />
......
......@@ -7,7 +7,6 @@ using Coscine.ApiCommons;
using Coscine.ApiCommons.Exceptions;
using Coscine.ApiCommons.Factories;
using Microsoft.AspNetCore.Mvc;
using Microsoft.SharePoint;
using System;
using System.Collections.Generic;
using System.Linq;
......@@ -17,16 +16,18 @@ namespace Coscine.Api.Project.Controllers
public class ProjectController : Controller
{
private readonly Authenticator _authenticator;
private readonly List<IProjectAction> projectActions;
private readonly List<IProjectAction> _projectActions;
private readonly ProjectModel _projectModel;
public ProjectController()
{
_authenticator = new Authenticator(this, Program.Configuration);
projectActions = new List<IProjectAction>()
_projectActions = new List<IProjectAction>()
{
new PIDAction(),
new SharePointSiteAction()
};
_projectModel = new ProjectModel();
}
[Route("[controller]")]
......@@ -34,8 +35,7 @@ namespace Coscine.Api.Project.Controllers
{
return Ok(_authenticator.ValidateAndExecute((user) =>
{
ProjectModel projectModel = new ProjectModel();
return projectModel.GetAllWhere((project) =>
return _projectModel.GetAllWhere((project) =>
(from projectRole in project.ProjectRolesProjectIdIds
where projectRole.User == user
&& projectRole.Role.DisplayName == "Owner"
......@@ -49,9 +49,15 @@ namespace Coscine.Api.Project.Controllers
{
return Ok(_authenticator.ValidateAndExecute((user) =>
{
ProjectModel projectModel = new ProjectModel();
var project = projectModel.GetById(Guid.Parse(id));
return new ProjectObject(project.Id, project.Description, project.DisplayName, project.Organization, project.StartDate, project.EndDate, project.Keywords);
var project = _projectModel.GetById(Guid.Parse(id));
if (_projectModel.CanSeeProject(user, project))
{
return new ProjectObject(project.Id, project.Description, project.DisplayName, project.Organization, project.StartDate, project.EndDate, project.Keywords);
}
else
{
throw new UnauthorizedAccessException("User is not allowed to see given project Id!");
}
}));
}
......@@ -61,11 +67,10 @@ namespace Coscine.Api.Project.Controllers
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))
var project = _projectModel.GetById(Guid.Parse(id));
if(_projectModel.OwnsProject(user, project))
{
return projectModel.UpdateByObject(project, projectObject);
return _projectModel.UpdateByObject(project, projectObject);
}
else
{
......@@ -80,8 +85,15 @@ namespace Coscine.Api.Project.Controllers
return base.Ok(_authenticator.ValidateAndExecute((user) =>
{
ProjectObject projectObject = ObjectFactory<ProjectObject>.DeserializeFromStream(Request.Body);
ProjectModel projectModel = new ProjectModel();
var project = projectModel.StoreFromObject(projectObject, user);
var project = _projectModel.StoreFromObject(projectObject, user);
if (projectObject.ParentId != null
&& projectObject.ParentId != new Guid()
&& _projectModel.OwnsProject(user, _projectModel.GetById(projectObject.ParentId))) // for now, only an owner can add subprojects to projects
{
SubProjectModel subProjectModel = new SubProjectModel();
subProjectModel.LinkSubProject(projectObject.ParentId, project.Id);
}
FireEvents((projectAction, projectEventArgs) => projectAction.OnProjectCreate(project, projectEventArgs));
......@@ -91,24 +103,10 @@ namespace Coscine.Api.Project.Controllers
private void FireEvents(Action<IProjectAction, ProjectEventArgs> eventAction)
{
var sharePointSite = Request.Headers["Referer"];
SPUserToken systemAccount = SPUserToken.SystemAccount;
using (SPSite site = new SPSite(sharePointSite, systemAccount))
ProjectEventArgs projectEventArgs = new ProjectEventArgs(Program.Configuration, new object[0]);
foreach (var projectAction in _projectActions)
{
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);
}
}
eventAction(projectAction, projectEventArgs);
}
}
}
......
......@@ -12,10 +12,12 @@ namespace Coscine.Api.Project.Controllers
public class ResourceController : Controller
{
private readonly Authenticator _authenticator;
private readonly ResourceModel _resourceModel;
public ResourceController()
{
_authenticator = new Authenticator(this, Program.Configuration);
_resourceModel = new ResourceModel();
}
[Route("[controller]")]
......@@ -23,8 +25,7 @@ namespace Coscine.Api.Project.Controllers
{
return Ok(_authenticator.ValidateAndExecute((user) =>
{
ResourceModel resourceModel = new ResourceModel();
return resourceModel.GetAllWhere((resource) =>
return _resourceModel.GetAllWhere((resource) =>
(from projectResource in resource.ProjectResourceResourceIdIds
where (from projectRole in projectResource.Project.ProjectRolesProjectIdIds
where projectRole.User == user
......@@ -41,9 +42,8 @@ namespace Coscine.Api.Project.Controllers
{
return Ok(_authenticator.ValidateAndExecute((user) =>
{
ResourceModel resourceModel = new ResourceModel();
var resource = resourceModel.GetById(Guid.Parse(id));
if (resourceModel.OwnsResource(user, resource))
var resource = _resourceModel.GetById(Guid.Parse(id));
if (_resourceModel.OwnsResource(user, resource))
{
//TODO: Find out why resource.Type is not set
if(resource.Type == null)
......@@ -66,11 +66,10 @@ namespace Coscine.Api.Project.Controllers
return Ok(_authenticator.ValidateAndExecute((user) =>
{
ResourceObject resourceObject = ObjectFactory<ResourceObject>.DeserializeFromStream(Request.Body);
ResourceModel resourceModel = new ResourceModel();
var resource = resourceModel.GetById(Guid.Parse(id));
if (resourceModel.OwnsResource(user, resource))
var resource = _resourceModel.GetById(Guid.Parse(id));
if (_resourceModel.OwnsResource(user, resource))
{
return resourceModel.UpdateByObject(resource, resourceObject);
return _resourceModel.UpdateByObject(resource, resourceObject);
}
else
{
......@@ -90,8 +89,7 @@ namespace Coscine.Api.Project.Controllers
var project = projectModel.GetById(Guid.Parse(projectId));
if (projectModel.OwnsProject(user, project))
{
ResourceModel resourceModel = new ResourceModel();
var resource = resourceModel.StoreFromObject(resourceObject);
var resource = _resourceModel.StoreFromObject(resourceObject);
projectModel.AddResource(project, resource);
......
using Coscine.Api.Project.Models;
using Coscine.Api.Project.ReturnObjects;
using Coscine.ApiCommons;
using Microsoft.AspNetCore.Mvc;
using System;
using System.Linq;
namespace Coscine.Api.Project.Controllers
{
public class SubProjectController : Controller
{
private readonly Authenticator _authenticator;
private readonly SubProjectModel _subProjectModel;
public SubProjectController()
{
_authenticator = new Authenticator(this, Program.Configuration);
_subProjectModel = new SubProjectModel();
}
[HttpGet("[controller]/{parentId}")]
public IActionResult Get(string parentId)
{
return Ok(_authenticator.ValidateAndExecute((user) =>
{
Guid parentGuid = new Guid(parentId);
ProjectModel projectModel = new ProjectModel();
if (projectModel.CanSeeProject(user, projectModel.GetById(parentGuid)))
{
var subProjects = _subProjectModel.GetAllWhere((subProjectM) => (subProjectM.ProjectId == parentGuid))
.Select((subProject) => projectModel.GetById(subProject.SubProjectId))
.Select((project) => new ProjectObject(project.Id, project.Description, project.DisplayName, project.Organization, project.StartDate, project.EndDate, project.Keywords, parentGuid));
return subProjects;
}
else
{
throw new UnauthorizedAccessException("User is not allowed to create a subproject for the given project id!");
}
}));
}
}
}
......@@ -47,6 +47,16 @@ namespace Coscine.Api.Project.Models
return projectRole;
}
public bool CanSeeProject(User user, Coscine.Database.Model.Project project)
{
return DatabaseConnection.ConnectToDatabase((db) => (from relation in db.ProjectRoles
where relation.Project == project
&& relation.User == user
&& (relation.Role.DisplayName == "Owner"
|| relation.Role.DisplayName == "Member")
select relation).Any());
}
public bool OwnsProject(User user, Coscine.Database.Model.Project project)
{
return DatabaseConnection.ConnectToDatabase((db) => (from relation in db.ProjectRoles
......
using System;
using System.Linq.Expressions;
using Coscine.ApiCommons.Models;
using Coscine.Database.Model;
using LinqToDB;
namespace Coscine.Api.Project.Models
{
public class SubProjectModel : DatabaseModel<SubProject>
{
public SubProjectModel() : base(Program.Configuration)
{
}
public override Expression<Func<SubProject, Guid>> GetIdFromObject()
{
return databaseObject => databaseObject.RelationId;
}
public override ITable<SubProject> GetITableFromDatabase(CoscineDB db)
{
return db.SubProjects;
}
public void LinkSubProject(Guid parentId, Guid childId)
{
Insert(new SubProject()
{
ProjectId = parentId,
SubProjectId = childId,
});
}
public override void SetObjectId(SubProject databaseObject, Guid id)
{
databaseObject.RelationId = id;
}
}
}
......@@ -45,8 +45,8 @@
<Reference Include="Consul, Version=0.7.2.6, Culture=neutral, PublicKeyToken=20a6ad9a81df1d95, processorArchitecture=MSIL">
<HintPath>..\packages\Consul.0.7.2.6\lib\net45\Consul.dll</HintPath>
</Reference>
<Reference Include="Coscine.Action, Version=1.0.0.0, Culture=neutral, processorArchitecture=MSIL">
<HintPath>..\packages\Coscine.Action.1.0.0\lib\net461\Coscine.Action.dll</HintPath>
<Reference Include="Coscine.Action, Version=1.1.1.0, Culture=neutral, processorArchitecture=MSIL">
<HintPath>..\packages\Coscine.Action.1.1.1\lib\net461\Coscine.Action.dll</HintPath>
</Reference>
<Reference Include="Coscine.ApiCommons, Version=1.0.0.0, Culture=neutral, processorArchitecture=MSIL">
<HintPath>..\packages\Coscine.ApiCommons.1.0.0\lib\net461\Coscine.ApiCommons.dll</HintPath>
......@@ -353,7 +353,6 @@
<Reference Include="Microsoft.Net.Http.Headers, Version=2.2.0.0, Culture=neutral, PublicKeyToken=adb9793829ddae60, processorArchitecture=MSIL">
<HintPath>..\packages\Microsoft.Net.Http.Headers.2.2.0\lib\netstandard2.0\Microsoft.Net.Http.Headers.dll</HintPath>
</Reference>
<Reference Include="Microsoft.SharePoint, Version=16.0.0.0, Culture=neutral, PublicKeyToken=71e9bce111e9429c, processorArchitecture=MSIL" />
<Reference Include="Microsoft.Win32.Registry, Version=4.1.1.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a, processorArchitecture=MSIL">
<HintPath>..\packages\Microsoft.Win32.Registry.4.5.0\lib\net461\Microsoft.Win32.Registry.dll</HintPath>
</Reference>
......@@ -566,12 +565,14 @@
<ItemGroup>
<Compile Include="Controllers\ProjectController.cs" />
<Compile Include="Controllers\ResourceController.cs" />
<Compile Include="Controllers\SubProjectController.cs" />
<Compile Include="Models\ProjectModel.cs" />
<Compile Include="Models\ProjectResourceModel.cs" />
<Compile Include="Models\ProjectRoleModel.cs" />
<Compile Include="Models\ResourceModel.cs" />
<Compile Include="Models\ResourceTypeModel.cs" />
<Compile Include="Models\RoleModel.cs" />
<Compile Include="Models\SubProjectModel.cs" />
<Compile Include="Models\UserModel.cs" />
<Compile Include="Program.cs" />
<Compile Include="Properties\AssemblyInfo.cs" />
......
using Coscine.ApiCommons.ReturnObjects;
using Coscine.Api.Project.Models;
using Coscine.ApiCommons.ReturnObjects;
using System;
namespace Coscine.Api.Project.ReturnObjects
......@@ -16,7 +17,9 @@ namespace Coscine.Api.Project.ReturnObjects
public DateTime EndDate { get; set; }
public string Keywords { get; set; }
public ProjectObject(Guid id, string description, string displayName, string organisation, DateTime startDate, DateTime endDate, string keywords)
public Guid ParentId { get; set; }
public ProjectObject(Guid id, string description, string displayName, string organisation, DateTime startDate, DateTime endDate, string keywords, Guid parentId = new Guid())
{
Id = id;
Description = description;
......@@ -25,7 +28,7 @@ namespace Coscine.Api.Project.ReturnObjects
StartDate = startDate;
EndDate = endDate;
Keywords = keywords;
ParentId = parentId;
}
}
}
......@@ -3,7 +3,7 @@
<package id="AutoMapper" version="8.0.0" targetFramework="net472" />
<package id="AutoMapper.Extensions.Microsoft.DependencyInjection" version="6.0.0" targetFramework="net472" />
<package id="Consul" version="0.7.2.6" targetFramework="net472" />
<package id="Coscine.Action" version="1.0.0" targetFramework="net472" />
<package id="Coscine.Action" version="1.1.1" targetFramework="net472" />
<package id="Coscine.ApiCommons" version="1.0.0" targetFramework="net472" />
<package id="Coscine.Configuration" version="1.4.0" targetFramework="net472" />
<package id="Coscine.Database" version="1.4.0" targetFramework="net472" />
......
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