Exemplo n.º 1
0
        private async Task ValidateUserRole(ProfileCreatable model)
        {
            var role = await _roleRepository.GetByAsync(r => r.RoleName.ToLower() == model.Role.ToLower());

            if (role == null)
            {
                throw new ArgumentException("Unknown role", "Role");
            }
        }
Exemplo n.º 2
0
        /// <summary>
        /// Creates new user
        /// </summary>
        /// <returns></returns>
        public async Task <ResponseModel> CreateUserAsync(ProfileCreatable model)
        {
            using (HttpClient client = new HttpClient())
            {
                SetAuthHeader(client);
                var content  = new StringContent(JsonConvert.SerializeObject(model), Encoding.UTF8, "application/json");
                var response = await client.PostAsync($"{_options.ServiceUrl}/{Const.IndentityApi.UserEntity}", content);

                if (!response.IsSuccessStatusCode)
                {
                    throw new ApplicationException("Can not create user with current parameters");
                }

                return(new ResponseModel()
                {
                    StatusCode = HttpStatusCode.OK, Message = "User successefully created"
                });
            }
        }
Exemplo n.º 3
0
        /// <inheritdoc />
        public async Task <Profile> CreateUserAsync(ProfileCreatable model, string token = null)
        {
            if (model == null)
            {
                throw new ArgumentNullException(nameof(model));
            }

            try
            {
                var azureUser = model.Adapt <AzureUser>();

                if (await IsUserExist(GetUserEmail(azureUser)))
                {
                    throw new ArgumentException("User already exist", "User");
                }
                if (!string.IsNullOrWhiteSpace(model.Role) && !string.IsNullOrWhiteSpace(token))
                {
                    await ValidateUserRole(model);
                    await ValidationUserByRole(model, token);
                }

                var createdUser = await _azureClient.PostUser(model.Adapt <AzureUser>());

                var usersInCache = _cache.Get <List <AzureUser> >(Consts.Cache.UsersKey);
                usersInCache.Add(createdUser);
                _cache.Set(Consts.Cache.UsersKey, usersInCache);
                if (!string.IsNullOrEmpty(model.State))
                {
                    await _localtionService.SetState(model.State);
                }
                if (!string.IsNullOrEmpty(model.City) && !string.IsNullOrEmpty(model.State))
                {
                    await _localtionService.SetCity(model.City, model.State);
                }

                return(createdUser.Adapt <Profile>());
            }
            catch (ApplicationException)
            {
                throw;
            }
        }
Exemplo n.º 4
0
        public async Task <IActionResult> Post([FromBody][Required] ProfileCreatable userCreatable)
        {
            try
            {
                string token          = HttpContext.Request.Headers["Authorization"].ToString().Split(' ').LastOrDefault();
                var    userToResponse = await _userService.CreateUserAsync(userCreatable, token);

                return(Ok(userToResponse));
            }
            catch (ApplicationException ex)
            {
                return(BadRequest(ex.Message));
            }
            catch (ArgumentException ex)
            {
                return(BadRequest(new ValidationProblemDetails(new Dictionary <string, string[]>(new List <KeyValuePair <string, string[]> >
                {
                    new KeyValuePair <string, string[]>(ex.ParamName, new string[] { ex.Message })
                }))));
            }
        }
Exemplo n.º 5
0
 private async Task ValidationUserByRole(ProfileCreatable model, string token)
 {
     if (model.Role.ToLower() == Consts.Roles.SuperAdmin || model.Role.ToLower() == Consts.Roles.SupportAdmin)
     {
         await ValidationByCompany(model.CompanyId, token);
     }
     else if (model.Role.ToLower() == Consts.Roles.SalesRep)
     {
         await ValidationByCompany(model.CompanyId, token);
         await ValidationByBranche(model.BranchId, token);
     }
     else
     {
         if (model.CompanyId.HasValue)
         {
             await ValidationByCompany(model.CompanyId, token);
         }
         if (model.BranchId.HasValue)
         {
             await ValidationByBranche(model.BranchId, token);
         }
     }
 }