示例#1
0
        public virtual async Task <UserModel> PrepareUserModelAsync(TblUsers user)
        {
            var result = user == null ? new UserModel() : user.Adapt <UserModel>();

            if (user != null)
            {
                result.CurrentAvatarUrl = user.Avatar;
                result.IsAdmin          = await HttpContext.Current.GetOwinContext().GetUserManager <ApplicationUserManager>()
                                          .IsInRoleAsync(user.Id, "Admin");
            }
            result.CountriesList = await _countriesService.GetAsSelectListAsync();

            result.UserGroupsList = await _userGroupsService.GetAsSelectListAsync();

            return(result);
        }
        public virtual async Task <ActionResult> Editor(UserModel model, bool?saveAndContinue)
        {
            model.CountriesList = await _countriesService.GetAsSelectListAsync();

            model.UserGroupsList = await _userGroupsService.GetAsSelectListAsync();

            if (!ModelState.IsValid)
            {
                return(View(model));
            }

            var record   = _userModelFactory.PrepareTblUsers(model);
            var recordId = record.Id;

            try
            {
                if (model.Avatar != null)
                {
                    if (record.Id != null)
                    {
                        var olduser = await UserManager.FindByIdAsync(record.Id);

                        try
                        {
                            var oldAvatarFile = Server.MapPath(olduser.Avatar);
                            if (System.IO.File.Exists(oldAvatarFile))
                            {
                                System.IO.File.Delete(oldAvatarFile);
                            }
                        }
                        catch
                        { }
                    }
                    record.Avatar = model.Avatar.SaveToAppData();
                }

                if (model.Id == null)
                {
                    //Add new record
                    if (string.IsNullOrWhiteSpace(model.Password))
                    {
                        ModelState.AddModelError("Password", _localizationService.GetResource("PasswordCannotEmpty"));
                        return(View(model));
                    }

                    record.RegisterDate = DateTime.Now;
                    recordId            = await _usersService.AddAsync(record, model.Password);
                }
                else
                {
                    //Edit record
                    var oldUser = await UserManager.FindByIdAsync(model.Id);

                    record.RegisterDate      = oldUser.RegisterDate;
                    record.UserLastLoginDate = oldUser.UserLastLoginDate;
                    record.UserLatestIP      = oldUser.UserLatestIP;
                    if (model.Avatar == null)
                    {
                        record.Avatar = oldUser.Avatar;
                    }
                    await _usersService.UpdateAsync(record, model.Password);
                }

                await _usersService.SetUserRoleAsync(recordId, model.IsAdmin);
            }
            catch (Exception e)
            {
                var errorCode = ErrorLog.GetDefault(System.Web.HttpContext.Current).Log(new Error(e, System.Web.HttpContext.Current));
                ModelState.AddModelError("", string.Format(_localizationService.GetResource("ErrorOnOperation"), e.Message, errorCode));
                return(View(model));
            }

            if (saveAndContinue != null && saveAndContinue.Value)
            {
                return(RedirectToAction("Editor", "ManageUsers", new { id = recordId }));
            }

            return(Content(@"<script language='javascript' type='text/javascript'>
                                window.close();
                                window.opener.refreshUsersGrid();
                             </script>"));
        }