Пример #1
0
        public async Task <IActionResult> ChangePassword(changePasswordViewModel model)
        {
            if (ModelState.IsValid)
            {
                var account = await adminsService.GetAdminById(sessionService.User.Id);

                if (account != null)
                {
                    var password = hasherService.ComputeSha256Hash(model.CurrentPassword);
                    if (password == account.Password)
                    {
                        account.Password = hasherService.ComputeSha256Hash(model.NewPassword);

                        await adminsService.Update(account);

                        TempData[Constants.ChangePasswordSuccessMessage] = Messages.EditSuccess;
                        return(View());
                    }
                    else
                    {
                        TempData[Constants.ChangePasswordErrorMessage] = Messages.InCorrectPassword;
                        return(View(model));
                    }
                }
                else
                {
                    return(RedirectToAction(nameof(AccessDenied)));
                }
            }
            return(View(model));
        }
Пример #2
0
        public async Task <User> Register(UserRegisterDTO userRegisterDTO)
        {
            try
            {
                var user = _iMapper.Map <UserRegisterDTO, User>(userRegisterDTO);
                //HASH PASSWORD
                user.PasswordHash = _hasherService.ComputeSha256Hash(userRegisterDTO.Password);

                await _context.Users.AddAsync(user);

                await _context.SaveChangesAsync();

                return(user);
            }
            catch { return(null); }
        }
Пример #3
0
        public bool CheckLogin(string userName, string password, out string errorMessage, out User account)
        {
            errorMessage = null;

            //Check whether this account is exist or not
            var hashedPassword = hasherService.ComputeSha256Hash(password);

            account = repository.FirstOrDefault <User>(i => i.UserName.Trim() == userName.Trim() && i.Password == hashedPassword, new string[] { "Role" });

            if (account == null)
            {
                errorMessage = Messages.LoginFailed;
                return(false);
            }

            if (account.IsActive == false)
            {
                errorMessage = Messages.InactiveAccount;
                return(false);
            }

            return(true);
        }
Пример #4
0
        public bool CheckLogin(string username, string password, out string errorMessage, out Admin account)
        {
            errorMessage = null;

            //Check whether this account is exist or not
            var hashedPassword = hasherService.ComputeSha256Hash(password);

            account = repository.FirstOrDefault <Admin>(i => i.NationalId.Trim() == username.Trim() && i.Password == hashedPassword, new string[] { "PermissionGroups", "PermissionGroups.PermissionGroup" });

            if (account == null)
            {
                errorMessage = Messages.LoginFailed;
                return(false);
            }


            if (account.IsActive == false || account.ActivationStartDate.Date > DateTime.Now.Date || (account.ActivationEndDate.HasValue && account.ActivationEndDate.Value.Date <= DateTime.Now.Date))
            {
                errorMessage = Messages.InactiveAccount;
                return(false);
            }

            return(true);
        }
Пример #5
0
        public async Task <ActionResult> Create(AdminViewModel model)
        {
            try
            {
                ModelState.Remove("Ger_ActivationStartDate");
                ModelState.Remove("Ger_ActivationEndDate");

                if (ModelState.IsValid)
                {
                    if (await adminsService.IsNationalIdAlreadyExists(model.NationalId))
                    {
                        TempData[Constants.ErrorMessage] = Messages.NationalIdAlreadyExists;
                        await model.FillRolesList(adminsService);

                        return(View(model));
                    }
                    if (adminsService.IsEmailExists(model.Email))
                    {
                        TempData[Constants.ErrorMessage] = Messages.EmailAlreadyExists;
                        await model.FillRolesList(adminsService);

                        return(View(model));
                    }
                    if (adminsService.IsPhoneNumberExists(model.MobileNumber))
                    {
                        TempData[Constants.ErrorMessage] = Messages.MobileNumberAlreadyExists;
                        await model.FillRolesList(adminsService);

                        return(View(model));
                    }

                    if (Request.Form.Files?.Count > 0)
                    {
                        MemoryStream ms = new MemoryStream();
                        Request.Form.Files["ProfileImage"].CopyTo(ms);
                        model.ProfileImageData = ms.ToArray();
                        Request.Form.Files["SignatureImage"].CopyTo(ms);
                        model.SignatureImageData = ms.ToArray();

                        ms.Close();
                        ms.Dispose();

                        model.ProfileImage = await SaveFile(Request.Form.Files["ProfileImage"]);

                        model.SignatureImage = await SaveFile(Request.Form.Files["SignatureImage"]);
                    }

                    var admin = new Admin
                    {
                        FirstName           = model.FirstName,
                        SecondName          = model.SecondName,
                        ThirdName           = model.ThirdName,
                        LastName            = model.LastName,
                        NationalId          = model.NationalId,
                        EmplyeeNo           = model.EmplyeeNo,
                        MobileNumber        = model.MobileNumber,
                        Email               = model.Email,
                        IsActive            = model.IsActive,
                        ActivationStartDate = model.ActivationStartDate,
                        ActivationEndDate   = model.ActivationEndDate,
                        Title               = model.Title,
                        CanApprove          = model.CanApprove,
                        ProfileImage        = model.ProfileImage,
                        SignatureImage      = model.SignatureImage,
                        IsManager           = model.IsManager,
                        DepartmentCode      = model.DepartmentCode,
                        ProfileImageData    = model.ProfileImageData,
                        SignatureImageData  = model.SignatureImageData,

                        Password = hasherService.ComputeSha256Hash(model.MobileNumber),

                        Roles = model.SelectedRolesIds.Select(i => new AdminRole {
                            RoleId = i
                        }).ToList()
                    };

                    await adminsService.Add(admin, sessionService.User.Id);

                    TempData[Constants.SuccessMessage] = Messages.CreateSuccess;

                    return(RedirectToAction("Index", "Admins"));
                }

                await model.FillRolesList(adminsService);

                return(View(model));
            }
            catch (Exception ex)
            {
                TempData[Constants.ErrorMessage] = Messages.ResourceManager.GetString(ex.Message);

                await model.FillRolesList(adminsService);

                return(View(model));
            }
        }
Пример #6
0
        private async Task Seed(IconicFundDbContext context, IWebHostEnvironment _hostingEnvironment, IHasherService hasherService)
        {
            if (!await context.Roles.AnyAsync())
            {
                context.Roles.Add(new Role {
                    Id = Guid.Parse(Constants.MainAdminRoleId), Name = "مدير النظام"
                });
                //context.Roles.Add(new Role { Id = Guid.Parse(Constants.SaftyRoleId), Name = "السلامة" });
                //context.Roles.Add(new Role { Id = Guid.Parse(Constants.LiftsRoleId), Name = "المصاعد" });
                //context.Roles.Add(new Role { Id = Guid.Parse(Constants.EngineeringOfficeRoleId), Name = "المكاتب الهندسية" });

                await context.SaveChangesAsync();
            }

            if (!await context.Admins.AnyAsync())
            {
                context.Admins.Add(new Admin
                {
                    Id           = Guid.Parse(Constants.MainAdminId),
                    FirstName    = "مدير",
                    LastName     = "النظام",
                    MobileNumber = "567123432",
                    Email        = "*****@*****.**",
                    EmplyeeNo    = "1",
                    NationalId   = "1",
                    Password     = hasherService.ComputeSha256Hash("mainadmin"),
                    CanApprove   = true,

                    IsActive            = true,
                    ActivationStartDate = DateTime.Now,
                    ActivationEndDate   = null,

                    Roles = new List <AdminRole> {
                        new AdminRole {
                            RoleId = Guid.Parse(Constants.MainAdminRoleId)
                        }
                    }
                });

                await context.SaveChangesAsync();
            }

            //if (!context.SaftyOffices.Any())
            //{
            //    context.SaftyOffices.Add(new SaftyOffice { Name = "مركز السلامة الميدانية بالمنطقة المركزية " });
            //    context.SaftyOffices.Add(new SaftyOffice { Name = "مركز السلامة الميدانية بالشوقية" });
            //    context.SaftyOffices.Add(new SaftyOffice { Name = "مركز السلامة الميدانية بالعزيزية" });
            //    context.SaftyOffices.Add(new SaftyOffice { Name = "مركز السلامة الميدانية بالمعابدة" });
            //    context.SaftyOffices.Add(new SaftyOffice { Name = "مركز السلامة الميدانية بالشرايع" });

            //    await context.SaveChangesAsync();
            //}

            if (!context.Nationalities.Any() && !context.Cities.Any() && !context.Regions.Any())
            {
                var seedPath             = Path.Combine(_hostingEnvironment.ContentRootPath, "Seed");
                var CitiesJSONtxt        = File.ReadAllText(Path.Combine(seedPath, "Cities.json"));
                var NationalitiesJSONtxt = File.ReadAllText(Path.Combine(seedPath, "Nationalities.json"));
                var RegionsJSONtxt       = File.ReadAllText(Path.Combine(seedPath, "Regions.json"));

                var Cities        = JsonConvert.DeserializeObject <CityViewModels>(CitiesJSONtxt);
                var Nationalities = JsonConvert.DeserializeObject <NationalitiesViewModel>(NationalitiesJSONtxt);
                var Regions       = JsonConvert.DeserializeObject <RegionsViewModel>(RegionsJSONtxt);
                context.Database.OpenConnection();
                try
                {
                    await context.Database.ExecuteSqlRawAsync("SET IDENTITY_INSERT dbo.Cities ON");

                    context.Cities.AddRange(Cities.Cities);
                    await context.SaveChangesAsync();

                    await context.Database.ExecuteSqlRawAsync("SET IDENTITY_INSERT dbo.Cities OFF");


                    await context.Database.ExecuteSqlRawAsync("SET IDENTITY_INSERT dbo.Regions ON");

                    context.Regions.AddRange(Regions.Regions);
                    await context.SaveChangesAsync();

                    await context.Database.ExecuteSqlRawAsync("SET IDENTITY_INSERT dbo.Regions OFF");


                    await context.Database.ExecuteSqlRawAsync("SET IDENTITY_INSERT dbo.Nationalities ON");

                    context.Nationalities.AddRange(Nationalities.Nationalities);
                    await context.SaveChangesAsync();

                    await context.Database.ExecuteSqlRawAsync("SET IDENTITY_INSERT dbo.Nationalities OFF");
                }
                finally
                {
                    context.Database.CloseConnection();
                }

                //context.Cities.AddRange(Cities.Cities);
                //context.Nationalities.AddRange(Nationalities.Nationalities);
                //context.Regions.AddRange(Regions.Regions);

                //await context.SaveChangesAsync();
            }
        }