Esempio n. 1
0
        public async Task <ActionResult> Register(RegisterViewModel model)
        {
            if (ModelState.IsValid)
            {
                var user = new ApplicationUser()
                {
                    UserName = model.UserName, FirstName = model.FirstName, LastName = model.LastName, Email = model.Email
                };
                var result = await UserManager.CreateAsync(user, model.Password);

                if (result.Succeeded)
                {
                    string defaultRole = UserRole.HR.ToString();
                    if (model.IsAdmin)
                    {
                        defaultRole = UserRole.Admin.ToString();
                    }
                    UserManager.AddToRole(user.Id, defaultRole);
                    var curUser = UserManager.FindByName(User.Identity.Name);
                    var task    = new Task(() => { MailUtility.SendAdminHRAccountMail(model.FirstName, model.UserName, model.Password, model.Email, curUser.Email, curUser.FirstName); });
                    task.Start();
                    ListDataService.RefreshLists();
                    //await SignInAsync(user, isPersistent: false);
                    return(RedirectToAction("Index", "Home"));
                }
                else
                {
                    AddErrors(result);
                }
            }

            // If we got this far, something failed, redisplay form
            return(View(model));
        }
Esempio n. 2
0
 //
 // GET: /AddEmployee/
 public ActionResult Index()
 {
     ListDataService.RefreshLists();
     return(View());
 }
Esempio n. 3
0
 public JsonResult UpdateEmployeeData(EmployeeVM employeeVM, FormCollection frm)
 {
     try
     {
         if (!string.IsNullOrWhiteSpace(employeeVM.Employee.EmpCode))
         {
             if (IsExistEmpCode(employeeVM.Employee.EmpCode, employeeVM.Employee.EmpId))
             {
                 return(Json("Employee Code already exist!", JsonRequestBehavior.AllowGet));
             }
         }
         using (IDbConnection cn = new SqlConnection(_connectionString))
         {
             cn.Open();
             if (employeeVM.Employee.EmpId > 0)
             {
                 employeeVM.Employee.DateTimeStampRecord(DbChangeType.Update);
                 employeeVM.Employee.IsActive = true;
                 cn.Update <Employee>(employeeVM.Employee);
                 SaveEmpSupIds(cn, employeeVM.Employee.EmpId, employeeVM.SupIds);
                 string userName    = cn.GetList <AspNetUsers>().Where(x => x.Email == employeeVM.Employee.Email).FirstOrDefault().UserName;
                 var    user        = UserManager.FindByName(userName);
                 string role        = UserManager.GetRoles(user.Id)[0];
                 string defaultRole = UserRole.Employee.ToString();
                 if (employeeVM.Employee.IsSupervisor)
                 {
                     defaultRole = UserRole.Supervisor.ToString();
                 }
                 UserManager.RemoveFromRole(user.Id, role);
                 UserManager.AddToRole(user.Id, defaultRole);
                 user.FirstName = employeeVM.Employee.EmpFirstName;
                 user.LastName  = employeeVM.Employee.EmpLastName;
                 UserManager.Update(user);
             }
             else
             {
                 employeeVM.Employee.DateTimeStampRecord(DbChangeType.Create);
                 employeeVM.Employee.IsActive = true;
                 cn.Insert <Employee>(employeeVM.Employee);
                 if (employeeVM.Employee.EmpId > 0)
                 {
                     SaveEmpSupIds(cn, employeeVM.Employee.EmpId, employeeVM.SupIds);
                     LeaveStatus leaveStatus = new LeaveStatus();
                     leaveStatus.EmpId = employeeVM.Employee.EmpId;
                     cn.Insert <LeaveStatus>(leaveStatus);
                     var user = new ApplicationUser()
                     {
                         UserName = employeeVM.Employee.Email, FirstName = employeeVM.Employee.EmpFirstName, LastName = employeeVM.Employee.EmpLastName, Email = employeeVM.Employee.Email
                     };
                     var result = UserManager.Create(user, employeeVM.Password.Trim());
                     if (result.Succeeded)
                     {
                         string defaultRole = UserRole.Employee.ToString();
                         if (employeeVM.Employee.IsSupervisor)
                         {
                             defaultRole = UserRole.Supervisor.ToString();
                         }
                         UserManager.AddToRole(user.Id, defaultRole);
                         var curUser = UserManager.FindByName(User.Identity.Name);
                         var task    = new Task(() => { MailUtility.SendAccountMail(employeeVM.Employee.EmpFirstName, employeeVM.Password.Trim(), employeeVM.Employee.Email, curUser.Email, curUser.FirstName); });
                         task.Start();
                         //MailUtility.SendAccountMail(employeeVM.Employee.EmpFirstName, employeeVM.Password, employeeVM.Employee.Email, curUser.Email, curUser.FirstName);
                     }
                     else
                     {
                         cn.Delete <Employee>(employeeVM.Employee);
                         return(Json("Email already exist!", JsonRequestBehavior.AllowGet));
                     }
                 }
             }
             cn.Close();
         }
         ListDataService.RefreshLists();
     }
     catch (Exception ex)
     {
         ConfigLogger.Instance.LogError(ex);
         return(Json("Some error occurred.", JsonRequestBehavior.AllowGet));
     }
     return(Json("Success", JsonRequestBehavior.AllowGet));
 }