Beispiel #1
0
        public IActionResult Authenticate(AuthenticationData authenticationData)
        {
            IActionResult response = Unauthorized();

            try
            {
                authenticationData.PhoneNumber = _phoneNumberFormatter.ConvertToInternationalFormat(authenticationData.PhoneNumber, _configuration["PhoneNumberRegion"]);
            }
            catch (Exception ex)
            {
                return(StatusCode(500, new { error = ex.Message }));
            }

            IdentityUser user = _dbContext.Users.FirstOrDefault(u => u.PhoneNumber.Equals(authenticationData.PhoneNumber));

            if (user is default(IdentityUser))
            {
                return(response);
            }

            var signInResult = _signInManager.CheckPasswordSignInAsync(user, authenticationData.Password, false).Result;

            if (signInResult.Succeeded)
            {
                var claims = CreateClaims(user);

                var tokenString = BuildToken(claims);

                response = Ok(tokenString);
            }

            return(response);
        }
        public async Task <ActionResult <Tenant> > Post([FromBody] JObject jObj)
        {
            var tenant = jObj.ToObject <Tenant>();

            tenant.DbContext = _dbContext;

            var password = jObj["Password"].Value <string>();

            try
            {
                tenant.PhoneNumber = _phoneNumberFormatter.ConvertToInternationalFormat(tenant.PhoneNumber, _configuration["PhoneNumberRegion"]);
            }
            catch (Exception ex)
            {
                return(StatusCode(500, new { error = ex.Message }));
            }

            tenant.UserName = tenant.PhoneNumber.Replace(" ", string.Empty);

            var dormitoryIdClaim = User.Claims.FirstOrDefault(cl => cl.Type.Equals("DormitoryId"));

            if (dormitoryIdClaim is default(Claim))
            {
                return(StatusCode(500, new { error = "DormitoryId of current user not found." }));
            }

            tenant.DormitoryId = Convert.ToInt32(dormitoryIdClaim.Value);

            TenantValidator validator = new(_dbContext);

            var result = validator.Validate(tenant);

            if (!result.IsValid)
            {
                return(StatusCode(500, new { errors = result.Errors }));
            }

            tenant.Priority = CalculatePriority(tenant);

            using var transaction = _dbContext.Database.BeginTransaction();

            var creationResult = await _userManager.CreateAsync(tenant, password);

            if (!creationResult.Succeeded)
            {
                return(StatusCode(500, new { errors = creationResult.Errors }));
            }

            var role = await _roleManager.FindByNameAsync(TenantRoleName);

            if (role is null)
            {
                return(StatusCode(500, new { error = $"Role `{TenantRoleName}` not found." }));
            }

            var addToRoleResult = _userManager.AddToRoleAsync(tenant, role.Name).Result;

            if (!addToRoleResult.Succeeded)
            {
                return(StatusCode(500, new { errors = addToRoleResult.Errors }));
            }

            var claimsCreationResult = await _userManager.AddClaimsAsync(tenant, tenant.GenerateClaims());

            if (!claimsCreationResult.Succeeded)
            {
                return(StatusCode(500, new { errors = claimsCreationResult.Errors }));
            }

            tenant.Dormitory.Tenants.Add(tenant);

            try
            {
                _dbContext.SaveChanges();
                transaction.Commit();
            }
            catch (Exception ex)
            {
                return(StatusCode(500, new { error = ex.Message }));
            }

            return(StatusCode(201, new { id = tenant.Id }));
        }
Beispiel #3
0
        public async Task <ActionResult <object> > Create([FromForm] string phoneNumber, [FromForm] string password, [FromForm] int dormitoryId)
        {
            var dormitory = _dbContext.Dormitories.FirstOrDefault(d => d.Id.Equals(dormitoryId));

            if (dormitory is null)
            {
                return(NotFound());
            }

            try
            {
                phoneNumber = _phoneNumberFormatter.ConvertToInternationalFormat(phoneNumber, _configuration["PhoneNumberRegion"]);
            }
            catch (Exception ex)
            {
                return(StatusCode(500, new { error = ex.Message }));
            }

            IdentityUser dormitoryAdministrator = new()
            {
                UserName    = phoneNumber.Replace(" ", string.Empty),
                PhoneNumber = phoneNumber,
            };

            using var transaction = _dbContext.Database.BeginTransaction();

            var creationResult = await _userManager.CreateAsync(dormitoryAdministrator, password);

            if (!creationResult.Succeeded)
            {
                return(StatusCode(500, new { errors = creationResult.Errors }));
            }

            var role = await _roleManager.FindByNameAsync(DormitoryAdministratorRoleName);

            if (role is null)
            {
                return(StatusCode(500, new { error = $"Role `{DormitoryAdministratorRoleName}` not found." }));
            }

            var addToRoleResult = _userManager.AddToRoleAsync(dormitoryAdministrator, role.Name).Result;

            if (!addToRoleResult.Succeeded)
            {
                return(StatusCode(500, new { errors = addToRoleResult.Errors }));
            }

            dormitory.Administrators.Add(dormitoryAdministrator);

            var claim = new Claim(type: DormitoryIdClaimType, value: dormitoryId.ToString());

            var createClaimResult = await _userManager.AddClaimAsync(dormitoryAdministrator, claim);

            if (!createClaimResult.Succeeded)
            {
                return(StatusCode(500, new { errors = createClaimResult.Errors }));
            }

            try
            {
                _dbContext.SaveChanges();
                transaction.Commit();
            }
            catch (Exception ex)
            {
                return(StatusCode(500, new { error = ex.Message }));
            }

            return(StatusCode(201, new { id = dormitoryAdministrator.Id }));
        }