Skip to content
Snippets Groups Projects
Select Git revision
  • master
  • new_rng
  • unstable
  • many_sweeps
  • hdf5
  • pemonts
6 results

setup.py

Blame
  • Code owners
    Assign users and groups as approvers for specific file changes. Learn more.
    ProjectRoleController.cs 6.50 KiB
    using Coscine.Action;
    using Coscine.Action.EventArgs;
    using Coscine.Action.Implementations.User;
    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 List<IUserAction> _userActions;
            private readonly ProjectRoleModel _projectRoleModel;
    
            public ProjectRoleController()
            {
                _authenticator = new Authenticator(this, Program.Configuration);
                _userActions = new List<IUserAction>()
                {
                    new SPGroupAction()
                };
                _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) =>
                            (projectRole.ProjectId == projectIdGuid)
                        ).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);
                            }
                            return new ProjectRoleObject(projectRole.ProjectId, new UserObject(userInst.Id, userInst.DisplayName, userInst.EmailAddress), new RoleObject(role.Id, role.DisplayName));
                        });
                    }
                    else
                    {
                        throw new UnauthorizedAccessException("User is not allowed to list all users to the given project!");
                    }
                }));
            }
    
            //Get all roles for current user and given project