public void init()
 {
     repository                = new RepositoryMock();
     dataService               = new DataService(repository);
     navigationService         = new NavigationServiceMock();
     registerEmployeeViewModel = new RegisterEmployeeViewModel(dataService, navigationService);
 }
        public async Task <IActionResult> RegisterEmployeeCompany(RegisterEmployeeViewModel model)
        {
            model.Password        = generatePasswordService.CreatePassword();
            model.PasswordConfirm = model.Password;
            if (ModelState.IsValid)
            {
                User user = new User {
                    Email = model.Email, UserName = userService.CreateLogin(model), IsPasswordChanged = false, IsTwoFactorOn = true
                };
                var result = await employeeService.CreateEmployee(user, model);

                if (result.Succeeded)
                {
                    employeeService.CreateEmployeeInfo(user, model);
                    StringBuilder message = new StringBuilder("ваш логин:  " + user.UserName + "  ", 120);;
                    message.AppendLine(" ваш пароль:" + model.Password);
                    await SendMessage("Регистрация в интернет банкинге", message.ToString(), user);

                    return(RedirectToAction("CompanyInfo", "Company", new { id = model.CompanyId }));
                }
                else
                {
                    foreach (var error in result.Errors)
                    {
                        ModelState.AddModelError(string.Empty, error.Description);
                    }
                }
            }
            return(View(model));
        }
Ejemplo n.º 3
0
        public async Task CreateAsync(RegisterEmployeeViewModel viewModel)
        {
            var employee = new EmployeeModel
            {
                EmployeeId    = viewModel.EmployeeId,
                GlobalId      = viewModel.GlobalId,
                CardId        = viewModel.CardId,
                EmployeeType  = viewModel.EmployeeType,
                Title         = viewModel.Title,
                TitleThai     = viewModel.TitleThai,
                FirstName     = viewModel.FirstName,
                LastName      = viewModel.LastName,
                FirstNameThai = viewModel.FirstNameThai,
                LastNameThai  = viewModel.LastNameThai,
                Gender        = viewModel.Gender,
                Height        = viewModel.Height,
                Hand          = viewModel.Hand,
                BirthDate     = viewModel.BirthDate,
                HireType      = viewModel.HireType,
                HireDate      = viewModel.HireDate,
                AvailableFlag = true,
                ChangedDate   = DateTime.Now,
            };

            await _employeeService.AddAsync(employee);
        }
        public IActionResult AddEmployee()
        {
            RegisterEmployeeViewModel r = new RegisterEmployeeViewModel();

            foreach (var role in Rolemanager.Roles)
            {
                if (User.IsInRole("Super Admin") || User.IsInRole("Admin") && role.Name != "Admin" && role.Name != "Super Admin")
                {
                    UserRoles user = new UserRoles()
                    {
                        RoleId     = role.Id,
                        RoleName   = role.Name,
                        isSelected = false
                    };
                    r.Roles.Add(user);
                }
            }
            foreach (Claim claim in ClaimStore.claimstore)
            {
                UserClaim userClaim = new UserClaim()
                {
                    ClaimType  = claim.Type,
                    ClaimValue = claim.Value,
                    isSelected = false,
                };
                r.Claims.Add(userClaim);
            }

            ViewBag.cities = util.getCities();
            ViewBag.Stores = util.GetAllStores();
            return(View(r));
        }
        public string CreateLogin(RegisterEmployeeViewModel model)
        {
            model.SecondName = Transliteration.Front(model.SecondName);
            model.FirstName  = Transliteration.Front(model.FirstName);
            bool isUnique = false;

            rnd = new Random();
            string fName = "";
            string sName = model.SecondName;
            string login = "";
            int    i     = 0;

            while (!isUnique)
            {
                if (i >= model.FirstName.Length)
                {
                    while (!isUnique)
                    {
                        string rand = rnd.Next(0, 999).ToString();
                        login    = fName + sName + rand;
                        isUnique = IsUniqueAsync(login).Result;
                        return(login);
                    }
                }
                fName += model.FirstName[i];
                i++;
                fName    = fName.ToLower();
                login    = fName + sName;
                isUnique = IsUniqueAsync(login).Result;
            }
            return(login);
        }
Ejemplo n.º 6
0
        public async Task <IActionResult> RegisterEmployee(RegisterEmployeeViewModel model)
        {
            if (ModelState.IsValid)
            {
                var user = new IdentityUser
                {
                    UserName = model.User.Email,
                    Email    = model.User.Email
                };

                var result = await _userManager.CreateAsync(user, model.User.Password);


                if (result.Succeeded)
                {
                    foreach (var role in model.RoleNames)
                    {
                        if (model.User.RoleName == role)
                        {
                            await _userManager.AddToRoleAsync(user, role);

                            _companyHandler.AddEmployee(user.Id, _companyHandler.GetById(model.CompanyId));
                            return(RedirectToAction("IndexEmployer", "Employer"));
                        }
                    }
                }

                foreach (var error in result.Errors)
                {
                    ModelState.AddModelError(string.Empty, error.Description);
                }
            }

            return(View(model));
        }
        public IActionResult RegisterEmployeeCompany(int companyId)
        {
            RegisterEmployeeViewModel employee = new RegisterEmployeeViewModel {
                CompanyId = companyId
            };

            return(View(employee));
        }
Ejemplo n.º 8
0
        public IActionResult RegisterEmployee(string id)
        {
            RegisterEmployeeViewModel model = new RegisterEmployeeViewModel {
                CompanyId = id
            };

            return(View(model));
        }
Ejemplo n.º 9
0
 public static Employee ToEmployeeDb(RegisterEmployeeViewModel employee, ApplicationUser applicationUser)
 {
     return(new Employee
     {
         Id = applicationUser.Id,
         FirstName = employee.FirstName,
         LastName = employee.LastName,
         CenterId = employee.CenterId
     });
 }
Ejemplo n.º 10
0
        public async Task <ActionResult> RegisterEmployeeUser(RegisterEmployeeViewModel model)
        {
            if (ModelState.IsValid)
            {
                // Check to see if this user is in the existing database
                LogicLayer.UserManager usrMgr = new LogicLayer.UserManager();
                try
                {
                    if (usrMgr.FindUser(model.Email))
                    {
                        // If this user already exists, we need to use the regular Register method
                        return(RedirectToAction("Register", "Account"));
                    }
                    // Not an existing user, create a DataObjects.User without roles
                    else
                    {
                        var employee = new DataObjects.User()
                        {
                            // These fields are need by sp_insert_user
                            Email       = model.Email,
                            FirstName   = model.GivenName,
                            LastName    = model.FamilyName,
                            PhoneNumber = model.PhoneNumber,
                        };
                        // Add the DataObjects.User to Employe table
                        if (usrMgr.AddUser(employee))
                        {
                            var employeeID = usrMgr.RetrieveUserIDFromEmail(model.Email);
                            var user       = new ApplicationUser()
                            {
                                EmployeeID = employeeID,
                                GivenName  = model.GivenName,
                                FamilyName = model.FamilyName,
                                UserName   = model.Email,
                                Email      = model.Email
                            };
                            var result = await UserManager.CreateAsync(user, "newuser");

                            if (result.Succeeded)
                            {
                                return(RedirectToAction("Index", "Admin"));
                            }
                            AddErrors(result);
                        }
                    }
                }
                catch
                {
                    // Creating Employee Failed
                    return(View(model));
                }
            }
            // ModelState was not valid
            return(View(model));
        }
        public async Task <IActionResult> GetUserById(string id)
        {
            var user = await Usermanager.FindByIdAsync(id);

            if (user != null)
            {
                RegisterEmployeeViewModel r = new RegisterEmployeeViewModel()
                {
                    id           = user.Id,
                    FullName     = user.FullName,
                    Email        = user.Email,
                    City         = util.getCities().FirstOrDefault(x => x.id == user.City).city,
                    PhoneNumber  = user.PhoneNumber,
                    Photopath    = user.Photopath,
                    StreetAdress = user.StreetAdress,
                };
                if (user.store_id != null)
                {
                    r.store_id = (int)user.store_id;
                }
                if (User.IsInRole("Super Admin") || User.IsInRole("Admin"))
                {
                    var userClaims = await Usermanager.GetClaimsAsync(user);

                    var userRoles = await Usermanager.GetRolesAsync(user);

                    if (user.store_id != null)
                    {
                        r.StoreName = util.GetAllStores().FirstOrDefault(x => x.store_id == user.store_id).StoreName;
                    }
                    r.addedBy = user.addedBy;
                    foreach (var claim in userClaims)
                    {
                        UserClaim u = new UserClaim()
                        {
                            ClaimType  = claim.Type,
                            ClaimValue = claim.Value
                        };
                        r.Claims.Add(u);
                    }
                    foreach (var role in userRoles)
                    {
                        UserRoles ro = new UserRoles()
                        {
                            RoleName = role
                        };
                        r.Roles.Add(ro);
                    }
                }
                return(View("~/Views/Home/Profile.cshtml", r));
            }
            //not Found
            ViewBag.Name = "User";
            return(View("ProductNotFound", id));
        }
Ejemplo n.º 12
0
        public ActionResult RegisterEmployee()
        {
            var viewModel = new RegisterEmployeeViewModel();

            viewModel.Roles = new List <string>()
            {
                RoleName.Technician, RoleName.Manager
            };

            return(View(viewModel));
        }
        public void ЕслиСоздаюСотрудника()
        {
            RegisterEmployeeViewModel model = ModelData.RegEmployeeSuccess();

            TextToElement("FName", model.FirstName);
            TextToElement("SName", model.SecondName);
            TextToElement("TName", model.MiddleName);
            TextToElement("Position", model.Position);
            TextToElement("Email", model.Email);
            Click("id", "SendClick");
        }
        public async Task <IActionResult> Register(RegisterEmployeeViewModel model)
        {
            if (ModelState.IsValid)
            {
                await _employeeViewModelService.CreateAsync(model);

                return(RedirectToAction(nameof(EmployeeList)));
            }

            return(View());
        }
Ejemplo n.º 15
0
        public void UpdateEmployee(User user, RegisterEmployeeViewModel model)
        {
            EmployeeInfo employeeInfo = context.EmployeeInfos.FirstOrDefault(e => e.UserId == model.UserId);

            employeeInfo.FirstName  = model.FirstName;
            employeeInfo.SecondName = model.SecondName;
            employeeInfo.MiddleName = model.MiddleName;
            employeeInfo.Position   = model.Position;
            context.EmployeeInfos.Update(employeeInfo);
            context.Users.Update(user);
            context.SaveChanges();
        }
Ejemplo n.º 16
0
        public async Task <ActionResult> RegisterEmployeeUser(RegisterEmployeeViewModel model)
        {
            if (ModelState.IsValid)
            {
                // check to see if the user is in the existing database
                LogicLayer.UserManager usrMgr = new LogicLayer.UserManager();
                try
                {
                    if (usrMgr.FindUser(model.Email))
                    {
                        // if the user already exists, we need to use the regular Register method
                        return(RedirectToAction("Register", "Account"));
                    }
                    else // not an existing user, create a DataObjects.User without any roles
                    {
                        var employee = new DataObjects.User()
                        {
                            // these fields are needed by sp_insert_user
                            Email       = model.Email,
                            FirstName   = model.GivenName,
                            LastName    = model.FamilyName,
                            PhoneNumber = model.PhoneNumber
                        };
                        if (usrMgr.CreateEmployee(employee.FirstName, employee.LastName, employee.PhoneNumber, employee.Email)) // add the DataObjects.User to Employee table
                        {
                            var employeeID = usrMgr.RetrieveUserIdFromEmail(model.Email);
                            var user       = new ApplicationUser // if it worked, create an Identity user
                            {
                                EmployeeID = employeeID,
                                GivenName  = model.GivenName,
                                FamilyName = model.FamilyName,
                                UserName   = model.Email,
                                Email      = model.Email
                            };
                            var result = await UserManager.CreateAsync(user, "newuser");

                            if (result.Succeeded) // go back to Admin/Index View
                            {
                                return(RedirectToAction("Index", "Admin"));
                            }
                            AddErrors(result);
                        }
                    }
                }
                catch
                {
                    // creating employee failed
                    return(View(model));
                }
            }
            // modelstate was not valid
            return(View(model));
        }
Ejemplo n.º 17
0
        public async Task <IActionResult> Profile()
        {
            var user = await Usermanager.GetUserAsync(User);

            if (user != null)
            {
                RegisterEmployeeViewModel r = new RegisterEmployeeViewModel()
                {
                    id           = user.Id,
                    FullName     = user.FullName,
                    Email        = user.Email,
                    City         = util.getCities().FirstOrDefault(x => x.id == user.City).city,
                    PhoneNumber  = user.PhoneNumber,
                    Photopath    = user.Photopath,
                    StreetAdress = user.StreetAdress,
                };
                if (await Usermanager.IsInRoleAsync(user, "Super Admin") || await Usermanager.IsInRoleAsync(user, "Admin"))
                {
                    var userClaims = await Usermanager.GetClaimsAsync(user);

                    var userRoles = await Usermanager.GetRolesAsync(user);

                    if (user.store_id != null)
                    {
                        r.StoreName = util.GetAllStores().FirstOrDefault(x => x.store_id == user.store_id).StoreName;
                    }
                    r.addedBy = user.addedBy;
                    foreach (var claim in userClaims)
                    {
                        UserClaim u = new UserClaim()
                        {
                            ClaimType  = claim.Type,
                            ClaimValue = claim.Value
                        };
                        r.Claims.Add(u);
                    }
                    foreach (var role in userRoles)
                    {
                        UserRoles ro = new UserRoles()
                        {
                            RoleName = role
                        };
                        r.Roles.Add(ro);
                    }
                }
                return(View(r));
            }
            else
            {
                RedirectToAction("Login");
            }
            return(View());
        }
Ejemplo n.º 18
0
        public async Task <ActionResult> RegisterEmployeeUser(RegisterEmployeeViewModel model)
        {
            if (ModelState.IsValid)
            {
                LogicLayer.UserManager usrMgr = new LogicLayer.UserManager();
                try
                {
                    if (usrMgr.FindUser(model.Email))
                    {
                        return(RedirectToAction("Register", "Account"));
                    }
                    else
                    {
                        var employee = new DataTransferObjects.PetUniverseUser
                        {
                            FirstName   = model.GivenName,
                            LastName    = model.FamilyName,
                            Email       = model.Email,
                            PhoneNumber = model.PhoneNumber,
                            Address1    = model.AddressLine1,
                            Address2    = model.AddressLine2,
                            City        = model.City,
                            State       = model.State,
                            ZipCode     = model.ZipCode
                        };
                        if (usrMgr.CreateNewUser(employee))
                        {
                            var employeeID = usrMgr.getUserByEmail(model.Email).PUUserID;
                            var user       = new ApplicationUser
                            {
                                EmployeeID = employeeID,
                                GivenName  = model.GivenName,
                                FamilyName = model.FamilyName,
                                UserName   = model.Email,
                                Email      = model.Email
                            };
                            var result = await UserManager.CreateAsync(user, "newuser");

                            if (result.Succeeded)
                            {
                                return(RedirectToAction("Index", "Admin"));
                            }
                            AddErrors(result);
                        }
                    }
                }
                catch (Exception)
                {
                    return(View(model));
                }
            }
            return(View(model));
        }
Ejemplo n.º 19
0
        public static RegisterEmployeeViewModel RegEmployeeSuccess()
        {
            RegisterEmployeeViewModel model = new RegisterEmployeeViewModel
            {
                FirstName  = "Rob",
                SecondName = "Stark",
                MiddleName = "Eddardovich",
                Email      = "*****@*****.**",
                Position   = "Boss",
            };

            return(model);
        }
        public async Task <ActionResult> Edit(RegisterEmployeeViewModel model)
        {
            try
            {
                User user = await userService.FindUserById(model.UserId);

                user.Email         = model.Email;
                user.IsTwoFactorOn = model.TwoFactorOn;
                employeeService.UpdateEmployee(user, model);
                return(RedirectToAction("Index", "Employee", new { id = model.CompanyId }));
            }
            catch
            {
                return(View());
            }
        }
Ejemplo n.º 21
0
        public IActionResult NewEmployee(RegisterEmployeeViewModel registerEmployeeViewModel)
        {
            if (!_peopleRepository.IsUniqueLogin(registerEmployeeViewModel.Login, registerEmployeeViewModel.Email))
            {
                ModelState.AddModelError("", "Логин или Email уже занят!");
            }

            if (ModelState.IsValid)
            {
                _peopleRepository.AddHuman(registerEmployeeViewModel.Login, registerEmployeeViewModel.Email, Encryption.Encryption.GetHash(registerEmployeeViewModel.Password), registerEmployeeViewModel.Name,
                                           registerEmployeeViewModel.Surname, registerEmployeeViewModel.MiddleName, registerEmployeeViewModel.PhoneNumber, registerEmployeeViewModel.Role);
                return(RedirectToAction("Employees", "Staff"));
            }

            return(View(registerEmployeeViewModel));
        }
Ejemplo n.º 22
0
        public async Task <IActionResult> RegisterEmployeeAsync([Microsoft.AspNetCore.Mvc.FromBody] RegisterEmployeeViewModel model)
        {
            if (ModelState.IsValid)
            {
                var result = await _employeeService.RegisterEmployeeAsync(model);

                if (result.IsSuccess)
                {
                    return(Ok(result));
                }

                return(BadRequest(result));
            }

            return(BadRequest("Some properties are not valid")); // Status Code: 400
        }
Ejemplo n.º 23
0
        public async Task <ActionResult> RegisterEmployee(RegisterEmployeeViewModel model)
        {
            if (ModelState.IsValid)
            {
                //TODO: Add fields to user here so they will be saved to do the database
                var user = new AppUser
                {
                    UserName      = model.Email,
                    Email         = model.Email,
                    PhoneNumber   = model.PhoneNumber,
                    FirstName     = model.FirstName,
                    MiddleInitial = model.MiddleInitial,
                    LastName      = model.LastName,
                    EmployeeType  = model.EmployeeType,
                    Address       = model.Address,
                    City          = model.City,
                    State         = model.State,
                    Zip           = model.Zip,
                    DateofBirth   = DateTime.Now
                };
                var result = await UserManager.CreateAsync(user, model.Password);



                //TODO:  Once you get roles working, you may want to add users to roles upon creation
                // --OR--
                // await UserManager.AddToRoleAsync(user.Id, "Employee");


                if (result.Succeeded)
                {
                    await SignInManager.SignInAsync(user, isPersistent : false, rememberBrowser : false);

                    // For more information on how to enable account confirmation and password reset please visit http://go.microsoft.com/fwlink/?LinkID=320771
                    // Send an email with this link
                    // string code = await UserManager.GenerateEmailConfirmationTokenAsync(user.Id);
                    // var callbackUrl = Url.Action("ConfirmEmail", "Account", new { userId = user.Id, code = code }, protocol: Request.Url.Scheme);
                    // await UserManager.SendEmailAsync(user.Id, "Confirm your account", "Please confirm your account by clicking <a href=\"" + callbackUrl + "\">here</a>");

                    return(RedirectToAction("Index", "Home"));
                }
                AddErrors(result);
            }

            // If we got this far, something failed, redisplay form
            return(View(model));
        }
Ejemplo n.º 24
0
        public async Task <ActionResult> RegisterEmployee(RegisterEmployeeViewModel model)
        {
            if (ModelState.IsValid)
            {
                var user = new ApplicationUser {
                    UserName = model.Email, Email = model.Email, UserType = "Employee"
                };

                var result = await UserManager.CreateAsync(user, model.Password);

                if (result.Succeeded)
                {
                    //await SignInManager.SignInAsync(user, isPersistent: false, rememberBrowser: false);

                    // For more information on how to enable account confirmation and password reset please visit https://go.microsoft.com/fwlink/?LinkID=320771
                    // Send an email with this link
                    // string code = await UserManager.GenerateEmailConfirmationTokenAsync(user.Id);
                    // var callbackUrl = Url.Action("ConfirmEmail", "Account", new { userId = user.Id, code = code }, protocol: Request.Url.Scheme);
                    // await UserManager.SendEmailAsync(user.Id, "Confirm your account", "Please confirm your account by clicking <a href=\"" + callbackUrl + "\">here</a>");
                    UserManager.AddToRole(user.Id, "Employee");
                    Employee employee = new Employee()
                    {
                        FirstName         = model.FirstName,
                        LastName          = model.LastName,
                        Address           = model.Address,
                        PhoneNumber       = model.PhoneNumber,
                        ApplicationUserId = user.Id,
                        CNIC     = model.CNIC,
                        HireDate = model.HireDate,
                        Wage     = model.Wage
                    };

                    ApplicationDbContext db = new ApplicationDbContext();


                    db.employees.Add(employee);
                    Console.WriteLine(employee);
                    db.SaveChanges();

                    return(RedirectToAction("Index", "Home", null));
                }
                AddErrors(result);
            }

            // If we got this far, something failed, redisplay form
            return(View(model));
        }
        public async Task <ActionResult> RegisterEmployee(RegisterEmployeeViewModel model)
        {
            if (ModelState.IsValid)
            {
                var user = new ApplicationUser {
                    UserName = model.Email, Email = model.Email
                };

                var result = await UserManager.CreateAsync(user, model.Password);

                if (result.Succeeded)
                {
                    UserManager.AddToRole(user.Id, "Employee");
                    await SignInManager.SignInAsync(user, isPersistent : false, rememberBrowser : false);

                    // For more information on how to enable account confirmation and password reset please visit http://go.microsoft.com/fwlink/?LinkID=320771
                    // Send an email with this link
                    // string code = await UserManager.GenerateEmailConfirmationTokenAsync(user.Id);
                    // var callbackUrl = Url.Action("ConfirmEmail", "Account", new { userId = user.Id, code = code }, protocol: Request.Url.Scheme);
                    // await UserManager.SendEmailAsync(user.Id, "Confirm your account", "Please confirm your account by clicking <a href=\"" + callbackUrl + "\">here</a>");


                    try
                    {
                        EmployeeManager _employeeManager = new EmployeeManager();
                        if (_employeeManager.CreateEmployee(model.FirstName, model.LastName, model.PhoneNum, model.Zip, model.Email))
                        {
                            UserManager.AddClaim(user.Id, new Claim(ClaimTypes.GivenName, model.FirstName));
                            UserManager.AddClaim(user.Id, new Claim(ClaimTypes.Surname, model.LastName));
                        }
                    }
                    catch (Exception)
                    {
                        ViewBag.Message = "Bad registration. Invalid username or password.";
                        return(View());
                    }

                    return(RedirectToAction("Index", "Home"));
                }
                AddErrors(result);
            }

            // If we got this far, something failed, redisplay form
            ViewBag.Message = "No.";
            return(View(model));
        }
Ejemplo n.º 26
0
        //[ValidateAntiForgeryToken]
        public async Task <IActionResult> RegisterEmployee([FromBody] RegisterEmployeeViewModel employeeModel)
        {
            if (ModelState.IsValid)
            {
                var user = new ApplicationUser {
                    UserName = employeeModel.Email, Email = employeeModel.Email
                };
                var result = await _userManager.CreateAsync(user, employeeModel.Password);

                if (result.Succeeded)
                {
                    _logger.LogInformation("User created a new account with password.");

                    var createdEmployee = await _userManager.FindByEmailAsync(employeeModel.Email);

                    await _userManager.AddToRoleAsync(createdEmployee, "DonationCenterDoctor");

                    var employeeDb = Mappers.MapperRegisterEmployee.ToEmployeeDb(employeeModel, createdEmployee);
                    try
                    {
                        _employeeService.AddEmployee(employeeDb);


                        var code = await _userManager.GenerateEmailConfirmationTokenAsync(user);

                        //var callbackUrl = Url.EmailConfirmationLink(user.Id, code, Request.Scheme);
                        //await _emailSender.SendEmailConfirmationAsync(doctorModel.Email, callbackUrl);

                        //await _signInManager.SignInAsync(user, isPersistent: false);
                        _logger.LogInformation("User created a new account with password.");

                        //return RedirectToLocal(returnUrl);
                        return(Ok(employeeModel));
                    }
                    catch (Exception ex)
                    {
                        return(BadRequest(ex.Message));
                    }
                }
                AddErrors(result);
            }

            // If we got this far, something failed, redisplay form
            return(BadRequest("Register employee failed"));
        }
Ejemplo n.º 27
0
        public EmployeeInfo CreateEmployeeInfo(User user, RegisterEmployeeViewModel model)
        {
            bool         chief    = context.EmployeeInfos.FirstOrDefault(ei => ei.CompanyId == model.CompanyId && ei.Chief) == null;
            EmployeeInfo employee = new EmployeeInfo
            {
                UserId     = user.Id,
                FirstName  = model.FirstName,
                SecondName = model.SecondName,
                MiddleName = model.MiddleName,
                Position   = model.Position,
                CompanyId  = model.CompanyId,
                Chief      = chief
            };

            context.EmployeeInfos.Add(employee);
            context.SaveChanges();
            return(employee);
        }
Ejemplo n.º 28
0
        public async Task <ActionResult> RegisterEmployee(RegisterEmployeeViewModel model)
        {
            if (ModelState.IsValid)
            {
                var user = new ApplicationUser {
                    UserName = model.Email, Email = model.Email
                };
                var result = await UserManager.CreateAsync(user, model.Password);

                if (result.Succeeded)
                {
                    result = await UserManager.AddToRoleAsync(user.Id, model.Role);
                }

                if (result.Succeeded)
                {
                    var appUser = new AppUser()
                    {
                        EmailAddress = model.Email,
                        Id           = user.Id,
                        PhoneNumber  = model.PhoneNumber,
                        NameFirst    = model.NameFirst,
                        NameLast     = model.NameLast
                    };

                    _context.AppUsers.Add(appUser);
                    _context.SaveChanges();

                    // await SignInManager.SignInAsync(user, isPersistent: false, rememberBrowser: false);

                    // For more information on how to enable account confirmation and password reset please visit https://go.microsoft.com/fwlink/?LinkID=320771
                    // Send an email with this link
                    // string code = await UserManager.GenerateEmailConfirmationTokenAsync(user.Id);
                    // var callbackUrl = Url.Action("ConfirmEmail", "Account", new { userId = user.Id, code = code }, protocol: Request.Url.Scheme);
                    // await UserManager.SendEmailAsync(user.Id, "Confirm your account", "Please confirm your account by clicking <a href=\"" + callbackUrl + "\">here</a>");

                    return(RedirectToAction("Index", "Employee"));
                }
                AddErrors(result);
            }

            // If we got this far, something failed, redisplay form
            return(View(model));
        }
        //public ActionResult Details(int id)
        //{
        //    return View();
        //}

        public async Task <ActionResult> Edit(int id)
        {
            EmployeeInfo employee = employeeService.FindEmployeeById(id);
            User         user     = await userService.FindUserById(employee.UserId);

            RegisterEmployeeViewModel model = new RegisterEmployeeViewModel
            {
                Email       = user.Email,
                FirstName   = employee.FirstName,
                SecondName  = employee.SecondName,
                MiddleName  = employee.MiddleName,
                Position    = employee.Position,
                TwoFactorOn = user.IsTwoFactorOn,
                UserId      = user.Id,
                CompanyId   = employee.CompanyId
            };

            return(View(model));
        }
Ejemplo n.º 30
0
        public async Task <ActionResult> RegisterEmployee(RegisterEmployeeViewModel model)
        {
            if (ModelState.IsValid)
            {
                var user = new User
                {
                    UserName             = model.Email,
                    Email                = model.Email,
                    Surname              = model.SurName,
                    OrganizationalUnitId = model.Id,
                    Name           = model.UserName,
                    PasswordHashed = System.Web.Helpers.Crypto.SHA256(model.Password)
                };
                var result = await UserManager.CreateAsync(user, model.Password);


                // Roles
                var userStore   = new UserStore <User>(db);
                var userManager = new UserManager <User>(userStore);


                //
                if (result.Succeeded)
                {
                    userManager.AddToRole(user.Id, "employee");
                    //await SignInManager.SignInAsync(user, isPersistent: false, rememberBrowser: false);

                    // For more information on how to enable account confirmation and password reset please visit https://go.microsoft.com/fwlink/?LinkID=320771
                    // Send an email with this link
                    string code = await UserManager.GenerateEmailConfirmationTokenAsync(user.Id);

                    var callbackUrl = Url.Action("ConfirmEmail", "Account", new { userId = user.Id, code = code }, protocol: Request.Url.Scheme);
                    await UserManager.SendEmailAsync(user.Id, "Confirm your account", "Please confirm your account by clicking <a href=\"" + callbackUrl + "\">here</a>");

                    return(RedirectToAction("RegisterEmployeeStepTwo", "Account"));
                }
                AddErrors(result);
            }

            // If we got this far, something failed, redisplay form
            ViewBag.Id = new SelectList(db.OrganizationalUnits, "Id", "Name");
            return(View(model));
        }