public bool ChangePassword(string email, string password, string newPassword, out string errorMessage)
        {
            errorMessage = string.Empty;
            string oldHash = PEncryption.Encrypt(password);

            var user = FindOne(u => u.IsActive == true && u.Email.ToLower() == email.ToLower() && u.PasswordHash == oldHash);

            if (user != null)
            {
                string newHash = PEncryption.Encrypt(newPassword);
                if (oldHash == newHash)
                {
                    errorMessage = "Please enter new password";
                    return(false);
                }
                user.PasswordHash      = newHash;
                user.IsChangedPassword = true;
                Update(user);
                return(true);
            }
            else
            {
                errorMessage = "Incorrect password";
                return(false);
            }
        }
Esempio n. 2
0
        public AppUser Login(string email, string password)
        {
            var passHash = PEncryption.Encrypt(password);
            var user     = FindOne(u => u.Email == email && u.PasswordHash == passHash && u.IsDeleted != true);

            if (user != null)
            {
                Context.Entry(user).Reference(u => u.JobTitle).Load();
                Context.Entry(user).Reference(u => u.Department).Load();
            }
            return(user);
        }
Esempio n. 3
0
        public bool ResetPassword(int userId, string password, out string errorMessage)
        {
            errorMessage = string.Empty;

            var user = GetByID(userId);

            if (user != null)
            {
                string newHash = PEncryption.Encrypt(password);
                user.PasswordHash = newHash;
                Update(user);
                return(true);
            }
            else
            {
                errorMessage = "User not found";
                return(false);
            }
        }
        public JsonResult CreateUser(AddUserDto model)
        {
            if (!ModelState.IsValid)
            {
                return(Json(null));
            }
            else
            {
                var user = new AppUser
                {
                    PasswordHash = PEncryption.Encrypt(model.Password),
                    FullName     = model.Name,
                    Email        = model.Email,
                    PhoneNumber  = model.PhoneNumber,
                    JobTitleId   = model.JobTitle,
                    IsActive     = model.IsActive,
                };

                if (UnitOfWork.UserBL.ValidateUser(user, out string errorMessage))
                {
                    user.LnkUserRole = model.Roles.Where(r => r.HasValue).Select(role => new LnkUserRole
                    {
                        RoleId = role.GetValueOrDefault(),
                    }).ToList();

                    user.LnkUserBranch = model.Branch.Where(r => r.HasValue).Select(Branch => new LnkUserBranch
                    {
                        BranchId = Branch.GetValueOrDefault(),
                    }).ToList();

                    user.CreationDate = DateTime.Now;

                    UnitOfWork.UserBL.Add(user);

                    if (UnitOfWork.Complete() > 0)
                    {
                        return(Json(new
                        {
                            Success = true,
                            Message = "User added successfully",
                        }));
                    }
                    else
                    {
                        return(Json(new
                        {
                            Success = false,
                            Message = "Failed to add user",
                        }));
                    }
                }
                else
                {
                    return(Json(new
                    {
                        Success = false,
                        Message = errorMessage,
                    }));
                }
            }
        }
Esempio n. 5
0
        public JsonResult CreateUser(AddUserDto model)
        {
            if (!ModelState.IsValid)
            {
                return(Json(null));
            }
            else
            {
                var selectedIds = new List <int>();
                if (!string.IsNullOrEmpty(model.SelectedTerritories))
                {
                    selectedIds = model.SelectedTerritories
                                  .Split(',', StringSplitOptions.RemoveEmptyEntries)
                                  .Select(int.Parse)
                                  .ToList();
                }
                var user = new AppUser
                {
                    PasswordHash = PEncryption.Encrypt(model.Password),
                    FullName     = model.Name,
                    Email        = model.Email,
                    PhoneNumber  = model.PhoneNumber,
                    JobTitleId   = model.JobTitle,
                    IsActive     = model.IsActive,
                    DepartmentId = model.Department, // edit mo salah
                };

                if (UnitOfWork.AppUserBL.ValidateUser(user, out string errorMessage))
                {
                    var allTerritories = UnitOfWork.TerritoriesBL.GetAllActive();

                    var selectedTerritories = allTerritories.Where(t => selectedIds.Contains(t.TerritoryId)).ToList();
                    var grantedTerritories  = new List <Territories>();
                    for (int i = 0; i < selectedTerritories.Count; i++)
                    {
                        var territory = selectedTerritories[i];
                        GetParentTerritories(territory, allTerritories, ref grantedTerritories);
                    }

                    user.UserTerritory = grantedTerritories.Select(t => new UserTerritory
                    {
                        TerritoryId = t.TerritoryId
                    }).ToList();

                    user.LnkUserRole = model.Roles.Where(r => r.HasValue).Select(role => new LnkUserRole
                    {
                        RoleId = role.GetValueOrDefault(),
                    }).ToList();

                    user.CreatedBy    = LoggedUserId;
                    user.CreationDate = DateTime.Now;

                    UnitOfWork.AppUserBL.Add(user);

                    if (UnitOfWork.Complete(LoggedUserId) > 0)
                    {
                        return(Json(new
                        {
                            Success = true,
                            Message = "User added successfully",
                        }));
                    }
                    else
                    {
                        return(Json(new
                        {
                            Success = false,
                            Message = "Failed to add user",
                        }));
                    }
                }
                else
                {
                    return(Json(new
                    {
                        Success = false,
                        ModelError = errorMessage,
                    }));
                }
            }
        }