Skip to content
Snippets Groups Projects
Select Git revision
  • c6417cb3ca142e2bcef33969b16815db159cc205
  • master default protected
  • gitkeep
  • dev protected
  • Issue/2309-docs
  • Fix/xxxx-migrateLogin
  • Issue/2127-userEmailsDisplayed
  • uiv2
  • Hotfix/2130-uiv2ContactChange
  • Hotfix/2087-efNet6
  • Issue/1910-MigrationtoNET6.0
  • Sprint/2022-01
  • Sprint/2021-2022
  • Issue/1742-FixesForExternalUserInvitation
  • Sprint/2021-23
  • Hotfix/82-updateDepsOfAPIs
  • Sprint/2021-18
  • Product/1027-apiClientGenerator
  • Topic/1718-setupApiClient
  • Sprint/2021-11
  • Sprint/2021-08
  • v3.2.2
  • v3.2.1
  • v3.2.0
  • v3.1.0
  • v3.0.0
  • v2.3.2
  • v2.3.1
  • v2.3.0
  • v2.2.1
  • v2.2.0
  • v2.1.6
  • v2.1.5
  • v2.1.4
  • v2.1.3
  • v2.1.2
  • v2.1.1
  • v2.1.0
  • v2.0.1
  • v2.0.0
  • v1.8.2
41 results

UserController.cs

Blame
  • Code owners
    Assign users and groups as approvers for specific file changes. Learn more.
    UserController.cs 2.67 KiB
    using Coscine.Api.User.Models;
    using Coscine.Api.User.ReturnObjects;
    using Coscine.ApiCommons;
    using Coscine.ApiCommons.Factories;
    using Microsoft.AspNetCore.Mvc;
    using System;
    using System.Linq;
    using System.ComponentModel.DataAnnotations;
    using Microsoft.AspNetCore.Authorization;
    
    namespace Coscine.Api.User.Controllers
    {
        [Authorize]
        public class UserController : Controller
        {
            private readonly Authenticator _authenticator;
            private readonly UserModel _userModel;
    
            public UserController()
            {
                _authenticator = new Authenticator(this, Program.Configuration);
                _userModel = new UserModel();
            }
    
            [HttpPost("[controller]/email")]
            public IActionResult ChangeContactMail()
            {
                var user = _authenticator.GetUser();
    
                UserObject userObject = ObjectFactory<UserObject>.DeserializeFromStream(Request.Body);
                if (new EmailAddressAttribute().IsValid(userObject.EmailAddress))
                {
                    user.EmailAddress = userObject.EmailAddress;
                    return Ok(_userModel.Update(user));
                }
                else
                {
                    throw new FormatException("Incorrect E-Mail format!");
                }
            }
    
            [HttpGet("[controller]/user")]
            public IActionResult GetUser()
            {
                var user = _authenticator.GetUser();
                return Ok(new UserObject(user.Id, user.DisplayName, user.EmailAddress));
            }
    
            [HttpGet("[controller]/query/{queryString}/project/{projectId}")]
            public IActionResult Query(string queryString, string projectId)
            {
                var user = _authenticator.GetUser();
                string lowerQueryString = queryString.ToLower();
                Guid.TryParse(projectId, out Guid projectIdGuid);
                ProjectModel projectModel = new ProjectModel();
    
                if (projectModel.HasAccess(user, projectModel.GetById(projectIdGuid)))
                {
                    return Ok(_userModel.GetAllWhere((dbUser) =>
                        (dbUser.DisplayName.ToLower().Contains(lowerQueryString)
                        || dbUser.EmailAddress.ToLower().Contains(lowerQueryString))
                        && !((from projectRole in dbUser.ProjectRolesUserIdIds
                              where projectRole.ProjectId == projectIdGuid
                              select projectRole).Any()))
                            .Take(10)
                            .Select((dbUser) => new UserObject(dbUser.Id, dbUser.DisplayName, null)));
                }
                else
                {
                    throw new UnauthorizedAccessException("User is not allowed to query users with respect to given project!");