Пример #1
0
        public ActionResult Edit(UserModel model, int[] lenders)
        {
            try
            {
                if (!ModelState.IsValid)
                {
                    throw new Exception();
                }

                try
                {
                    DataRepository.UpdateUser(model);
                }
                catch
                {
                    return new HttpStatusCodeResult(HttpStatusCode.NotFound);
                }
                DataRepository.UpdateUserAssignments(model.username, lenders);

                return RedirectToAction("Index");
            }
            catch
            {
                #region Redisplay page

                ViewBag.active_menu_item_id = "menu-btn-users";
                var user = DataRepository.GetUsersWithDetails().Single(u => u.username == model.username);
                model.lastLogin = user.lastLogin;
                var dbLenders = DataRepository.GetLenders();
                ViewBag.Lenders = dbLenders;

                var selectedLenders = new List<LenderModel>();
                foreach (int lid in lenders)
                {
                    selectedLenders.Add(
                        new LenderModel
                        {
                            name = dbLenders.Where(dbl => dbl.id == lid).First().name,
                            id = lid
                        });
                }
                ViewBag.SelectedLenders = selectedLenders;

                #endregion

                return View(model);
            }
        }
Пример #2
0
        /// <summary>
        /// Returns all users from the database with every collectable detail
        /// </summary>
        /// <returns></returns>
        public static IEnumerable<UserModel> GetUsersWithDetails()
        {
            var result = new List<UserModel>();

            #region Get users data

            var dc = new BicikliDataClassesDataContext();
            var membershipUsers = Membership.GetAllUsers();
            foreach (MembershipUser mUser in membershipUsers)
            {
                var uModel = new UserModel();

                #region Get user data

                uModel.username = mUser.UserName;
                uModel.guid = (Guid)mUser.ProviderUserKey;
                uModel.email = mUser.Email;
                uModel.countOfLenders = GetLendersOfUser(uModel.guid).Count();
                uModel.lastLogin = mUser.LastLoginDate;
                uModel.isSiteAdmin = Roles.IsUserInRole(mUser.UserName, "SiteAdmin");
                uModel.isLockedOut = mUser.IsLockedOut;
                uModel.isApproved = mUser.IsApproved;

                #endregion

                result.Add(uModel);
            }

            #endregion

            return result;
        }
Пример #3
0
        /// <summary>
        /// Updates a user profile
        /// </summary>
        /// <param name="model"></param>
        public static void UpdateUser(UserModel model)
        {
            var mUser = Membership.GetUser(model.username);

            if (mUser.Email != model.email)
            {
                mUser.Email = model.email;
            }
            if (mUser.IsApproved != model.isApproved)
            {
                mUser.IsApproved = model.isApproved;
            }

            Membership.UpdateUser(mUser);

            if (mUser.IsLockedOut != model.isLockedOut)
            {
                if (model.isLockedOut)
                {
                    try
                    {
                        for (int i = 0; i < Membership.MaxInvalidPasswordAttempts; i++)
                        {
                            Membership.ValidateUser(mUser.UserName, "98zfd8vbd9fvbdfv9d8vz9b8dz9a8z89z9d8z9da8za98fdzd");
                        }
                    }
                    catch
                    {
                        //dummy
                    }
                }
                else
                {
                    mUser.UnlockUser();
                }
            }
            if (Roles.IsUserInRole(mUser.UserName, "SiteAdmin") != model.isSiteAdmin)
            {
                if (model.isSiteAdmin)
                {
                    Roles.AddUserToRole(mUser.UserName, "SiteAdmin");
                }
                else
                {
                    Roles.RemoveUserFromRole(mUser.UserName, "SiteAdmin");
                }
            }
            Membership.UpdateUser(mUser);
        }