Exemplo n.º 1
0
        public async Task <ActionResult> CreateUser(Models.CreateUserModel model)
        {
            if (ModelState.IsValid)
            {
                //BOSDataLayer.BackOfficeConn DB = new BOSDataLayer.BackOfficeConn();
                string UserName = model.UserName.Trim();

                try
                {
                    JneCommSitesDataLayer.JneCommSitesDataBaseEntities _dbContext = new JneCommSitesDataLayer.JneCommSitesDataBaseEntities();

                    /*if ((from p in _dbContext.AspNetUsers where p.UserName == UserName select p.Id).Any())
                     * {
                     *  throw new Exception("This user name is not available.");
                     * }*/

                    var potentialUser = await UserManager.FindByNameAsync(model.UserName);

                    if (potentialUser != null)
                    {
                        throw new Exception("UserName@This user name is already taken");
                    }

                    potentialUser = await UserManager.FindByEmailAsync(model.Email);

                    if (potentialUser != null)
                    {
                        throw new Exception("Email@This email is already taken");
                    }
                    MembershipCreateStatus createStatusResult = MembershipCreateStatus.ProviderError;

                    var userProfile = new ApplicationUser {
                        UserName = model.UserName, Email = model.Email
                    };
                    var result = await UserManager.CreateAsync(userProfile, model.Password);

                    if (!result.Succeeded)
                    {
                        throw new Exception(result.Errors.FirstOrDefault().ToString());
                    }
                    else
                    {
                        await UserManager.SendEmailAsync(userProfile.Id, "Welcome to JNECommunications LLC", "This is your Username: "******"User was not created, please try again.");
                        }

                        var queryRol = (from p in _dbContext.AspNetRoles
                                        where p.Name == model.UserGroup
                                        select p).FirstOrDefault();

                        newUserData.Id                   = user.Id;
                        newUserData.UserFirstName        = model.firstName;
                        newUserData.UserLastName         = model.LastName;
                        newUserData.NumDaysForPassChange = model.daysChangePass;
                        newUserData.vStateCode           = "TX";
                        newUserData.UserDescription      = model.Description;
                        newUserData.ForcePassChange      = model.forcePassChange;

                        _dbContext.T_UsersData.Add(newUserData);
                        user.AspNetRoles.Add(queryRol);
                        user.LockoutEnabled = model.lockedOutUser;

                        _dbContext.SaveChanges();
                        _dbContext.Dispose();

                        TempData["Message"] = "User was created successfully";
                        return(RedirectToAction("UsersIndex", "Maintenance"));
                    }
                }
                catch (Exception err)
                {
                    model._UserGroup = Helper.Helper.GetRoles();
                    string[] tokens = err.Message.Split('@');
                    if (tokens.Length > 1)
                    {
                        ModelState.AddModelError(tokens[0], tokens[1]);
                    }
                    else
                    {
                        ModelState.AddModelError(String.Empty, err.Message);
                    }
                }
            }
            model._UserGroup = Helper.Helper.GetRoles();
            return(View(model));
        }
Exemplo n.º 2
0
        public async Task <ActionResult> CreateEmployee(Models.EmployeeModel model)
        {
            if (ModelState.IsValid)
            {
                string UserName = model.UserName.Trim();

                try
                {
                    JneCommSitesDataLayer.JneCommSitesDataBaseEntities _dbContext = new JneCommSitesDataLayer.JneCommSitesDataBaseEntities();


                    var potentialUser = await UserManager.FindByNameAsync(model.UserName);

                    if (potentialUser != null)
                    {
                        throw new Exception("UserName@This user name is already taken");
                    }

                    potentialUser = await UserManager.FindByEmailAsync(model.Email);

                    if (potentialUser != null)
                    {
                        throw new Exception("Email@This email is already taken");
                    }
                    MembershipCreateStatus createStatusResult = MembershipCreateStatus.ProviderError;

                    var userProfile = new ApplicationUser {
                        UserName = model.UserName, Email = model.Email
                    };
                    var result = await UserManager.CreateAsync(userProfile, model.Password);

                    if (!result.Succeeded)
                    {
                        throw new Exception(result.Errors.FirstOrDefault().ToString());
                    }
                    else
                    {
                        await UserManager.SendEmailAsync(userProfile.Id, "Welcome to JNECommunications LLC", "This is your Username: "******"User was not created, please try again.");
                        }

                        var queryRol = (from p in _dbContext.AspNetRoles
                                        where p.Name == "CrewRole"
                                        select p).FirstOrDefault();

                        var queryCrewRol = (from p in _dbContext.T_CrewRoles
                                            where p.vCrewRoleName == model.UserCrewGroup
                                            select p).FirstOrDefault();

                        newUserData.Id            = user.Id;
                        newUserData.UserFirstName = model.firstName;
                        newUserData.UserLastName  = model.LastName;
                        //newUserData.NumDaysForPassChange = model.daysChangePass;
                        newUserData.UserDescription = "User for crew";
                        //newUserData.ForcePassChange = model.forcePassChange;
                        newUserData.vStateCode   = model.states;
                        newUserData.LaborHourPay = Convert.ToDecimal(model.laborHour);
                        newUserData.IsBilingual  = model.isBilingual;

                        _dbContext.T_UsersData.Add(newUserData);
                        user.AspNetRoles.Add(queryRol);
                        user.T_CrewRoles.Add(queryCrewRol);
                        user.LockoutEnabled = model.lockedOutUser;

                        _dbContext.SaveChanges();


                        foreach (var item in model._ListCertifications)
                        {
                            if (item.isActive)
                            {
                                JneCommSitesDataLayer.T_CertificationsByUserCrew newCertificationByUser = new JneCommSitesDataLayer.T_CertificationsByUserCrew();
                                newCertificationByUser.vCertificationName = item.certificationName;
                                newCertificationByUser.Id = user.Id; if (item.expirationTime != null)
                                {
                                    newCertificationByUser.dExpirationTime = Convert.ToDateTime(item.expirationTime);
                                }
                                if (item.documentToUpload != null)
                                {
                                    newCertificationByUser.vDocumentName = (model.UserName + "_" + item.certificationName + Path.GetExtension(item.documentToUpload.FileName)).Replace(" ", "");

                                    string path = System.IO.Path.Combine(Server.MapPath("~/Documents/Certifications/"), (model.UserName + "_" + item.certificationName + Path.GetExtension(item.documentToUpload.FileName)).Replace(" ", ""));
                                    // file is uploaded
                                    item.documentToUpload.SaveAs(path);
                                }
                                _dbContext.T_CertificationsByUserCrew.Add(newCertificationByUser);
                                _dbContext.SaveChanges();
                            }
                        }


                        _dbContext.Dispose();

                        TempData["Message"] = "User was created successfully";
                        return(RedirectToAction("CrewEmployeeIndex", "Administration"));
                    }
                }
                catch (Exception err)
                {
                    model._UserCrewGroup = Helper.Helper.GetSubRoles("CrewRole");
                    model._StatesList    = Helper.Helper.GetUSAStates();
                    string[] tokens = err.Message.Split('@');
                    if (tokens.Length > 1)
                    {
                        ModelState.AddModelError(tokens[0], tokens[1]);
                    }
                    else
                    {
                        ModelState.AddModelError(String.Empty, err.Message);
                    }
                }
            }
            model._UserCrewGroup = Helper.Helper.GetSubRoles("CrewRole");
            model._StatesList    = Helper.Helper.GetUSAStates();
            return(View(model));
        }