예제 #1
0
        public async Task <ApiResponse <string> > RequestRegistration(string email)
        {
            var response = new ApiResponse <string>();

            using (var tx = _registrationRequestRepo.Context.Database.BeginTransaction())
            {
                try
                {
                    var isEmailAlreadyRegistered = _userRepo.List(i => i.Email == email).Any();
                    if (isEmailAlreadyRegistered)
                    {
                        response.Success = false;
                        response.ErrorMesssage.Add(MtsResource.InvalidEmail);
                    }
                    if (response.Success)
                    {
                        var    registrationRequest = _registrationRequestRepo.List(i => i.Email == email).FirstOrDefault();
                        string token = RandomGenerator.GenerateString(50);

                        if (registrationRequest != null)
                        {
                            registrationRequest.Token = token;
                            await _registrationRequestRepo.Update(registrationRequest);
                        }
                        else
                        {
                            await _registrationRequestRepo.Save(new Entity.RegistrationRequest
                            {
                                Email       = email,
                                Token       = token,
                                CreatedDate = DateTime.UtcNow,
                                UpdatedDate = DateTime.UtcNow
                            });
                        }

                        _emailService.Send(new EmailBody
                        {
                            Content = GenerateRegistrationRequestBody(token, email),
                            From    = "*****@*****.**",
                            To      = email,
                            Subject = "Registration Request"
                        });

                        tx.Commit();
                    }
                }
                catch (Exception e)
                {
                    response.Success = false;
                    response.ErrorMesssage.Add(e.GetBaseException().Message);
                    tx.Rollback();
                }
            }

            return(response);
        }
예제 #2
0
        public async Task <Dto.ApiResponse <Dto.Role> > CreateRole(Dto.Role model)
        {
            var response = new Dto.ApiResponse <Dto.Role>();

            if (string.IsNullOrWhiteSpace(model.Name) || model.BusinessId == 0)
            {
                response.Success = false;
                response.ErrorMesssage.Add(MtsResource.InvalidRoleBusiness);
            }

            var isRoleExist = _roleRepo.List(i => i.Name == model.Name && i.BusinessId == model.BusinessId).Any();

            if (isRoleExist)
            {
                response.Success = false;
                response.ErrorMesssage.Add(MtsResource.RoleAlreadyExist);
            }

            if (response.Success)
            {
                var roleEntity = _mapper.Map <Entity.Role>(model);
                await _roleRepo.Save(roleEntity);

                response.DataResponse        = _mapper.Map <Dto.Role>(roleEntity);
                response.DataResponse.UserId = model.UserId;
            }

            return(response);
        }
예제 #3
0
        public async Task <ApiResponse <User> > RegisterUser(User user)
        {
            var response   = new ApiResponse <User>();
            var userEntity = _mapper.Map <Entity.User>(user);

            userEntity.IsActive = true;

            var businessEntity = _mapper.Map <Entity.Business>(user.Business);

            var isEmailExist = _userRepo.List(i => i.Email == user.Email).Any();

            if (isEmailExist)
            {
                response.Success = false;
                response.ErrorMesssage.Add(MtsResource.InvalidEmail);
            }

            var isBusinessNameExist = _businessRepo.List(i => i.Name == user.Business.Name).Any();

            if (isBusinessNameExist)
            {
                response.Success = false;
                response.ErrorMesssage.Add(MtsResource.InvalidBusinessName);
            }

            if (response.Success)
            {
                using (var tx = _registrationRequestRepo.Context.Database.BeginTransaction())
                {
                    try
                    {
                        userEntity.Password = _crypto.CalculateHash(user.Password);
                        await _userRepo.Save(userEntity);

                        await _businessRepo.Save(businessEntity);

                        await _userBusinessRepo.Save(new Entity.UserBusiness
                        {
                            BusinessId = businessEntity.Id,
                            UserId     = userEntity.Id
                        });

                        user                  = _mapper.Map <User>(userEntity);
                        user.Business         = _mapper.Map <Business>(businessEntity);
                        response.DataResponse = user;

                        tx.Commit();
                    }
                    catch (Exception e)
                    {
                        response.Success = false;
                        response.ErrorMesssage.Add(e.GetBaseException().Message);
                        tx.Rollback();
                    }
                }
            }


            return(response);
        }
예제 #4
0
        public async Task <dto.UserSecurityProfile> RetrieveUserSecurityProfile(int userid)
        {
            var user     = _userRepo.List(i => i.Id == userid).FirstOrDefault();
            var response = new dto.UserSecurityProfile()
            {
                FirstName = user.FirstName,
                LastName  = user.LastName,
                UserId    = user.Id
            };

            response.SecurityProfile = (await _userRepository.GetUserSecurityProfileAsync(userid)).Select(i => new dto.SecurityProfile
            {
                Name  = i.Name,
                Route = i.RouteAddress
            });

            return(response);
        }
예제 #5
0
        public async Task <Dto.ApiResponse <object> > CreateInitialAdminRole(Dto.Role model)
        {
            var response = new Dto.ApiResponse <object>();

            using (var tx = _roleRepo.Context.Database.BeginTransaction())
            {
                try
                {
                    //Create initial role for the user
                    var roleEntity         = _mapper.Map <Entity.Role>(model);
                    var createRoleResponse = await this.CreateRole(model);

                    if (createRoleResponse.Success)
                    {
                        //assign the role to the user
                        model = createRoleResponse.DataResponse;
                        var assignedRoleToUserResponse = await this.AssignRoleToUser(model);

                        if (assignedRoleToUserResponse.Success)
                        {
                            //assigned all of the feature to the user
                            var appFeatures = _appFeatureRepo.List();
                            foreach (var appFeature in appFeatures)
                            {
                                await this.AssignRoleApplicationFeatureForUser(new Dto.RoleApplicationFeature
                                {
                                    ApplicationFeatureId = appFeature.Id,
                                    RoleId     = model.Id,
                                    FullAccess = true,
                                    CreatedBy  = MtsResource.AdminCreateUpdateBy,
                                    UpdatedBy  = MtsResource.AdminCreateUpdateBy
                                });
                            }

                            tx.Commit();
                        }
                        else
                        {
                            response.Success       = false;
                            response.ErrorMesssage = assignedRoleToUserResponse.ErrorMesssage;
                        }
                    }
                    else
                    {
                        response.Success       = false;
                        response.ErrorMesssage = createRoleResponse.ErrorMesssage;
                    }
                }
                catch (Exception e)
                {
                    response.Success = false;
                    response.ErrorMesssage.Add(e.GetBaseException().Message);
                    tx.Rollback();
                }
            }
            return(response);
        }
예제 #6
0
        public async Task <Dto.ApiResponse <object> > AssignRoleApplicationFeatureForUser(Dto.RoleApplicationFeature model)
        {
            var response              = new Dto.ApiResponse <object>();
            var roleAppEntity         = _mapper.Map <Entity.RoleApplicationFeature>(model);
            var isRoleAlreadyAssigned = _roleAppRepo.List(i => i.RoleId == model.RoleId && i.ApplicationFeatureId == model.ApplicationFeatureId).Any();

            if (isRoleAlreadyAssigned)
            {
                response.Success = false;
                response.ErrorMesssage.Add(MtsResource.RoleAlreadyAssignToFeature);
            }

            if (response.Success)
            {
                await _roleAppRepo.Save(roleAppEntity);
            }
            return(response);
        }
예제 #7
0
        public async Task <Dto.ApiResponse <object> > AssignRoleToUser(Dto.Role model)
        {
            var response = new Dto.ApiResponse <object>();

            var isRoleAlreadyassignedToUser = _userRoleRepo.List(i => i.RoleId == model.Id && i.UserId == model.UserId).Any();

            if (isRoleAlreadyassignedToUser)
            {
                response.Success = false;
                response.ErrorMesssage.Add(MtsResource.RoleAlreadyAssignToUser);
            }

            var userRoleEntity = _mapper.Map <Entity.UserRole>(model);

            userRoleEntity.RoleId = model.Id;

            await _userRoleRepo.Save(userRoleEntity);

            return(response);
        }
예제 #8
0
        public async Task <ApiResponse <LoginDetail> > AuthenticateUser(UserLogin model)
        {
            var response = new ApiResponse <LoginDetail>();

            var userDetail = _userRepo.List(i => i.Email == model.Email).FirstOrDefault();
            var loginLog   = new Entity.LoginLog(model.IpAddress);

            if (userDetail != null && !userDetail.IsActive)
            {
                response.Success = false;
                response.ErrorMesssage.Add(MtsResource.AccountLocked);
                loginLog.Success = false;
            }

            if (response.Success)
            {
                if (userDetail == null || !_crypto.CheckMatch(userDetail.Password, model.Password))
                {
                    response.Success = false;
                    response.ErrorMesssage.Add(MtsResource.EmailPasswordNotFound);
                    if (userDetail != null)
                    {
                        loginLog.Success       = false;
                        loginLog.UserId        = userDetail.Id;
                        userDetail.ErrorCount += 1;
                        if (userDetail.ErrorCount == _config.MaxLoginErrorCount)
                        {
                            await DeactivateUser(userDetail);
                        }
                    }
                }
                if (response.Success)
                {
                    var userBusiness = _userBusinessRepo.List(i => i.UserId == userDetail.Id).FirstOrDefault();
                    if (userBusiness != null)
                    {
                        string chip = $"{model.Email}:{model.Password}";
                        loginLog.UserId       = userDetail.Id;
                        userDetail.ErrorCount = 0;
                        await _userRepo.Update(userDetail);

                        response.DataResponse = new LoginDetail()
                        {
                            BusinessId   = userBusiness.BusinessId,
                            Id           = userDetail.Id,
                            Name         = $"{userDetail.FirstName} {userDetail.LastName}",
                            RefreshToken = $"{_crypto.EncryptString(chip, _config.EncryptionKey)}.{_crypto.CalculateHash(chip)}",
                            AccessToken  = RandomGenerator.GenerateString(16)
                        };
                    }
                    else
                    {
                        response.Success = false;
                        response.ErrorMesssage.Add(MtsResource.EmailPasswordNotFound);
                    }
                }
            }

            await _loginLogRepo.Save(loginLog);

            return(response);
        }
예제 #9
0
파일: Program.cs 프로젝트: rhanzies62/mts
 public static void ListRecords()
 {
     var records   = repo.List();
     var hasRecord = records.Any();
 }