Exemplo n.º 1
0
        private async Task <LoginResultViewmodel> LoginWithSelectedAccount(Login login, UserDto user)
        {
            PendingLoginDto dbPendingLogin = await _pendingLoginDal.Find(new PendingLoginDto
            {
                UserUuid   = user.Uuid,
                AccessCode = login.LoginCode
            });

            if (dbPendingLogin == null || dbPendingLogin.ExpirationDate < DateTime.Now)
            {
                throw new UnauthorizedAccessException(nameof(login));
            }

            if (login.SelectedAccountRole > user.AccountRole)
            {
                throw new UnauthorizedAccessException();
            }

            user.AccountRole = login.SelectedAccountRole;
            await _pendingLoginDal.Remove(dbPendingLogin);

            await _pendingLoginDal.RemoveOutdated();

            AuthorizationTokensViewmodel tokens = await _jwtLogic.CreateJwt(user);

            return(new LoginResultViewmodel
            {
                Jwt = tokens.Jwt,
                RefreshToken = tokens.RefreshToken,
            });
        }
Exemplo n.º 2
0
        /// <summary>
        /// The user has an possibility to login with multiple account roles if the account is admin or site admin
        /// This is implemented so that the user does not need multiple accounts for every functionality
        /// </summary>
        /// <param name="user">The user from the database</param>
        /// <returns>The login result</returns>
        private async Task <LoginResultViewmodel> HandleMultipleAccountRolesLogin(UserDto user)
        {
            var pendingLogin = new PendingLoginDto
            {
                UserUuid = user.Uuid
            };

            var userFromUserService = _rpcClient.Call <List <UserRabbitMqSensitiveInformation> >(new List <Guid>
            {
                user.Uuid
            }, RabbitMqQueues.FindUserQueue).FirstOrDefault();

            var email = new EmailRabbitMq
            {
                EmailAddress  = userFromUserService.Email,
                Subject       = "Login code",
                TemplateName  = "LoginMultiRole",
                KeyWordValues = new List <EmailKeyWordValue>
                {
                    new EmailKeyWordValue
                    {
                        Key   = "Username",
                        Value = user.Username
                    },
                    new EmailKeyWordValue
                    {
                        Key   = "LoginCode",
                        Value = pendingLogin.AccessCode.ToString()
                    }
                }
            };

            _publisher.Publish(new List <EmailRabbitMq> {
                email
            }, RabbitMqRouting.SendMail, RabbitMqExchange.MailExchange);

            await _pendingLoginDal.Remove(pendingLogin.UserUuid);

            await _pendingLoginDal.RemoveOutdated();

            await _pendingLoginDal.Add(pendingLogin);

            List <AccountRole> allAccountRoles = Enum.GetValues(typeof(AccountRole))
                                                 .Cast <AccountRole>()
                                                 .ToList();

            return(new LoginResultViewmodel
            {
                UserHasMultipleAccountRoles = true,
                SelectableAccountRoles = allAccountRoles
                                         .FindAll(aa => aa != AccountRole.Undefined && aa <= user.AccountRole)
            });
        }
Exemplo n.º 3
0
        public async Task Remove(Guid userUuid)
        {
            PendingLoginDto pendingLoginToRemove =
                await _context.PendingLogin
                .FirstOrDefaultAsync(pl => pl.UserUuid == userUuid);

            if (pendingLoginToRemove != null)
            {
                _context.PendingLogin.Remove(pendingLoginToRemove);
                await _context.SaveChangesAsync();
            }
        }
Exemplo n.º 4
0
 public async Task Remove(PendingLoginDto pendingLogin)
 {
     _context.PendingLogin.Remove(pendingLogin);
     await _context.SaveChangesAsync();
 }
Exemplo n.º 5
0
 public async Task <PendingLoginDto> Find(PendingLoginDto pendingLogin)
 {
     return(await _context.PendingLogin
            .FirstOrDefaultAsync(pl => pl.UserUuid == pendingLogin.UserUuid &&
                                 pl.AccessCode == pendingLogin.AccessCode));
 }
Exemplo n.º 6
0
        public async Task Add(PendingLoginDto pendingLogin)
        {
            await _context.PendingLogin.AddAsync(pendingLogin);

            await _context.SaveChangesAsync();
        }