Esempio n. 1
0
        public long AddMasterAccount(MasterAccountCreateEditModel modelParam)
        {
            var dto = _mapper.Map <MasterAccount>(modelParam);

            var entry = _masterAccountRepository.Query().FirstOrDefault(acc =>
                                                                        acc.Deleted && acc.AccountName == dto.AccountName);

            // restore existing account
            if (entry != null)
            {
                var existingAccount = entry;

                entry = _masterAccountRepository.GetEntry(new MasterAccount {
                    AccountAlias = dto.AccountAlias
                });
                if (entry != null)
                {
                    _actionContext.SetModelError("accountAlias", "Account alias is busy");
                }
                _actionContext.ThrowIfModelInvalid();

                existingAccount.Deleted      = false;
                existingAccount.AccountAlias = dto.AccountAlias;
                existingAccount.UpdatedById  = _identityService.GetIdentityId();
                return(_masterAccountRepository.Update(existingAccount).Id);
            }

            entry = _masterAccountRepository.GetEntry(new MasterAccount {
                AccountAlias = dto.AccountAlias
            });
            if (entry != null)
            {
                _actionContext.SetModelError("accountAlias", "Account alias is busy");
            }

            entry = _masterAccountRepository.GetEntry(new MasterAccount {
                AccountName = dto.AccountName
            });
            if (entry != null)
            {
                _actionContext.SetModelError("accountName", "Account name is busy");
            }

            _actionContext.ThrowIfModelInvalid();

            dto.CreatedById = _identityService.GetIdentityId();

            dto = _masterAccountRepository.Add(dto);

            return(dto.Id);
        }
Esempio n. 2
0
        public long ChangeRole(UserRoleModel modelParam)
        {
            // TODO: need add constraint in db and throw exception
            var dbUser = _userRepository.Query()
                         .Include(u => u.Roles)
                         .FirstOrDefault(u => u.Id == modelParam.Id);

            if (dbUser == null)
            {
                _actionContext.SetModelError("idUser", "User not found by id");
                _actionContext.ThrowIfModelInvalid();
            }

            var userRoles = dbUser.Roles.Select(r => r.Name).ToList();
            var allRoles  = _roleRepository.GetAll();

            // Check is valid client role list
            if (userRoles.Any(role => !allRoles.Select(r => r.Name).Contains(role)))
            {
                _actionContext.SetModelError("roles", "Invalid role list");
                _actionContext.ThrowIfModelInvalid();
            }

            // Add roles
            var newRoles = modelParam.Roles.Except(userRoles).ToList();

            if (newRoles.Any())
            {
                var additional = allRoles.Where(r => newRoles.Contains(r.Name)).ToList();
                additional.ForEach(add => dbUser.Roles.Add(add));
            }

            // Minus roles
            var removeRoles = userRoles.Except(modelParam.Roles).ToList();

            if (removeRoles.Any())
            {
                var forDeleting = allRoles.Where(r => removeRoles.Contains(r.Name)).ToList();
                dbUser.Roles = dbUser.Roles.Except(forDeleting).ToList();
            }

            _userRepository.Save(dbUser);

            return(modelParam.Id);
        }
Esempio n. 3
0
        public TradeAccountModel GetBySearchName(string searchName)
        {
            if (!Regex.IsMatch(searchName, Patterns.AccountName))
            {
                _actionContext.SetModelError("search", "Invalid search name format");
                _actionContext.ThrowIfModelInvalid();
            }

            var account = _tradeAccountRepository.GetBySearchName(searchName);

            if (account == null)
            {
                _actionContext.SetModelError("search", "Trade account not found");
                _actionContext.ThrowIfModelInvalid();
            }

            return(_mapper.Map <TradeAccountModel>(account));
        }
Esempio n. 4
0
        public UserModel LoginAs(LoginModel modelParam, string roleName)
        {
            // TODO: ardasovvadim: encrypt and decrypt password

            ValidateDomain(modelParam.Email);
            _actionContext.ThrowIfModelInvalid();

            var repositoryUser = _userRepository.GetByEmail(modelParam.Email);

            CheckCredentials(modelParam, repositoryUser, roleName);
            _actionContext.ThrowIfModelInvalid();

            var verificationCode = SecurityExtensions.GetVerificationCode();

            _logInfoService.AddVerificationLog(repositoryUser, verificationCode);

            _messengerService.SendSms(repositoryUser.Phone, $"Your Code: {verificationCode}");

            return(_mapper.Map <UserModel>(repositoryUser));
        }
Esempio n. 5
0
        public void CheckAndLogin(User user, int verificationCode)
        {
            var loginInfo = _logInfoRepository.GetLastByUserId(user.Id);

            if (loginInfo == null || loginInfo.IsUsed || loginInfo.ExpiryDate < DateTime.UtcNow)
            {
                _actionContext.SetModelError("phoneCode", "Verification code was used or expired");
            }
            if (loginInfo.VerificationCode != verificationCode)
            {
                _actionContext.SetModelError("phoneCode", "Verification code invalid");
            }

            _actionContext.ThrowIfModelInvalid();

            loginInfo.IsUsed    = true;
            loginInfo.LoginTime = DateTime.UtcNow;
            _logInfoRepository.Save(loginInfo);
        }