Пример #1
0
        public ActionResult Create(Models.CreateUserModel Model, FormCollection Form)
        {
            var act = new Action(() =>
            {
                var dbRoles = Repository.Roles;

                int c = Math.Min(Model.Roles.Count, dbRoles.Count);
                for (int i = 0; i < c; ++i)
                {
                    Model.Roles[i].Role = dbRoles[i];
                }

                var newUser      = new Entities.GlUser();
                newUser.Name     = Model.User.Name;
                newUser.Password = Model.User.Password;
                newUser.Email    = Model.User.Email;

                var roles = CreateEmptyModel().GetRoles(User.IsInRole(Algowe.Web.Global.AConst.RoleSuperAdmin));
                for (int i = 0; i < roles.Count; ++i) // suggestion: order of roles same as in Create.cshtml
                {
                    var role = roles[i];
                    var r    = Model.Roles[i];
                    if (r.Checked)
                    {
                        newUser.UserRoles.Add(new Entities.GlUserRole()
                        {
                            CurrentUser = newUser, CurrentRole = role.Role
                        });
                    }
                }
                Repository.CreateUser(newUser);
            });

            return(Create(act, CreateEmptyModel));
        }
Пример #2
0
        public ActionResult EditUser(string userName)
        {
            if (string.IsNullOrEmpty(userName))
            {
                return(RedirectToAction("UsersIndex", "Maintenance"));
            }
            JneCommSitesDataLayer.JneCommSitesDataBaseEntities _dbContext = new JneCommSitesDataLayer.JneCommSitesDataBaseEntities();

            var aspNetUserQuery = (from p in _dbContext.AspNetUsers
                                   where p.UserName == userName
                                   select p).FirstOrDefault();

            var rolQUery = (from p in _dbContext.AspNetRoles
                            from d in p.AspNetUsers
                            where d.Id == aspNetUserQuery.Id
                            select p).FirstOrDefault();

            var userInf = (from p in _dbContext.T_UsersData
                           where p.Id == aspNetUserQuery.Id
                           select p).First();

            Models.CreateUserModel model = new Models.CreateUserModel();

            model._UserGroup      = Helper.Helper.GetRoles();
            model.UserGroup       = rolQUery.Name;
            model.UserName        = aspNetUserQuery.UserName;
            model.firstName       = userInf.UserFirstName;
            model.LastName        = userInf.UserLastName;
            model.daysChangePass  = Convert.ToInt16(userInf.NumDaysForPassChange);
            model.Email           = aspNetUserQuery.Email;
            model.lockedOutUser   = aspNetUserQuery.LockoutEnabled;
            model.forcePassChange = Convert.ToBoolean(userInf.ForcePassChange);
            return(View(model));
        }
Пример #3
0
        //
        // GET: /Users/Create
        public ActionResult Create(string action)
        {
            ServiceClient sc = Sas.GetAuthenticatedServiceClient(this, action);
            SeniorDesign.Models.CreateUserModel usrMod = new Models.CreateUserModel();
            usrMod.Groups = sc.GetAllGroups(new PagedDataRequest { StartRow = 0, EndRow = 100000 }).Data.ToDictionary(p => p.Name, p => p.Id);

            return View(usrMod);
        }
Пример #4
0
 public ActionResult CreateUser()
 {
     ViewBag.Message = TempData["Message"];
     Models.CreateUserModel model = new Models.CreateUserModel();
     model._UserGroup      = Helper.Helper.GetRoles();
     model.daysChangePass  = 365;
     model.Email           = "";
     model.lockedOutUser   = true;
     model.forcePassChange = true;
     return(View(model));
 }
Пример #5
0
        public async Task <ActionResult> EditUser(Models.CreateUserModel model)
        {
            if (ModelState.IsValid)
            {
                JneCommSitesDataLayer.JneCommSitesDataBaseEntities _dbContext = new JneCommSitesDataLayer.JneCommSitesDataBaseEntities();

                var aspNetUserQuery = (from p in _dbContext.AspNetUsers
                                       where p.UserName == model.UserName
                                       select p).FirstOrDefault();

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

                aspNetUserQuery.AspNetRoles.Clear();

                aspNetUserQuery.AspNetRoles.Add(actualRol);
                if (!string.IsNullOrEmpty(model.Password))
                {
                    var token = await UserManager.GeneratePasswordResetTokenAsync(aspNetUserQuery.Id);

                    var result = await UserManager.ResetPasswordAsync(aspNetUserQuery.Id, token, model.Password);
                }
                var userInf = (from p in _dbContext.T_UsersData
                               where p.Id == aspNetUserQuery.Id
                               select p).First();

                userInf.UserFirstName        = model.firstName;
                userInf.UserLastName         = model.LastName;
                userInf.NumDaysForPassChange = model.daysChangePass;

                _dbContext.SaveChanges();

                return(RedirectToAction("UsersIndex", "Maintenance"));
            }
            return(View(model));
        }
Пример #6
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));
        }