예제 #1
0
        public ActionResult SaveStoreUsers(int id, LoginModel userName, String roleName)
        {
            var regexUtil = new RegexUtilities();

            if (!regexUtil.IsValidEmail(userName.UserName))
            {
                ModelState.AddModelError("UserName", "Invalid Email Address");
                return(View(userName));
            }


            int storeId = id;

            //if (ModelState.IsValid)
            {
                try
                {
                    WebSecurity.CreateUserAndAccount(userName.UserName, userName.Password);
                    Roles.AddUserToRole(userName.UserName, roleName);
                    int userId = 0;
                    // using (UsersContext db = new UsersContext())
                    {
                        UserProfile user = DbContext.UserProfiles.FirstOrDefault(u => u.UserName.ToLower() == userName.UserName.ToLower());
                        userId           = user.UserId;
                        user.FirstName   = userName.FirstName;
                        user.LastName    = userName.LastName;
                        user.PhoneNumber = userName.PhoneNumber;
                        user.CreatedDate = DateTime.Now;
                        DbContext.SaveChanges();
                    }

                    if (!roleName.Equals("SuperAdmin", StringComparison.InvariantCultureIgnoreCase))
                    {
                        var su = new StoreUser();
                        su.StoreId     = storeId;
                        su.UserId      = userId;
                        su.CreatedDate = DateTime.Now;
                        su.UpdatedDate = DateTime.Now;

                        StoreUserRepository.Add(su);
                        StoreUserRepository.Save();
                    }


                    return(RedirectToAction("Users", new { id = storeId }));
                }
                catch (MembershipCreateUserException e)
                {
                    ModelState.AddModelError("", "Exception:" + e.Message);
                }
            }
            return(RedirectToAction("Users", new { id = storeId }));
        }
예제 #2
0
        public virtual ActionResult SaveOrEditStoreUser(int storeId, LoginModel userName, String roleName = "")
        {
            storeId = GetStoreId(storeId);
            if (String.IsNullOrEmpty(roleName))
            {
                ModelState.AddModelError("UserName", "SELECT A ROLE PLEASE");
            }
            var store = this.StoreRepository.GetSingle(storeId);

            ViewBag.Store = store;

            ViewBag.Roles = DbContext.Roles.ToList();
            bool isSuperAdmin = false;

            try
            {
                isSuperAdmin = User.Identity.IsAuthenticated && Roles.GetRolesForUser(User.Identity.Name).Contains("SuperAdmin");
            }
            catch (Exception ex)
            {
                Logger.Error(ex, " StackTrace: " + ex.StackTrace, storeId, userName, roleName);
            }

            UserProfile user = DbContext.UserProfiles.FirstOrDefault(u => u.UserName.ToLower() == userName.UserName.ToLower());

            // Check if user already exists
            if (user == null)
            {
                WebSecurity.CreateUserAndAccount(userName.UserName, userName.Password);
                Roles.AddUserToRole(userName.UserName, roleName);

                var i = DbContext.UserProfiles.FirstOrDefault(u => u.UserName.ToLower() == userName.UserName.ToLower());

                i.FirstName     = userName.FirstName;
                i.LastName      = userName.LastName;
                i.PhoneNumber   = userName.PhoneNumber;
                i.CreatedDate   = DateTime.Now;
                i.LastLoginDate = DateTime.Now;
                DbContext.SaveChanges();

                if (!roleName.Equals("SuperAdmin", StringComparison.InvariantCultureIgnoreCase))
                {
                    StoreUser su = new StoreUser();
                    su.StoreId     = storeId;
                    su.UserId      = i.UserId;
                    su.State       = true;
                    su.Ordering    = 1;
                    su.CreatedDate = DateTime.Now;
                    su.UpdatedDate = DateTime.Now;

                    StoreUserRepository.Add(su);
                    StoreUserRepository.Save();
                }
            }
            else
            {
                user.UserName    = userName.UserName;
                user.FirstName   = userName.FirstName;
                user.LastName    = userName.LastName;
                user.PhoneNumber = userName.PhoneNumber;
                DbContext.SaveChanges();
            }


            return(RedirectToAction(isSuperAdmin ? "Users" : "Index", new { storeId = storeId }));
        }