Skip to content

Commit

Permalink
完成用户编辑。
Browse files Browse the repository at this point in the history
  • Loading branch information
majian159 committed Jul 22, 2015
1 parent 4c2cf34 commit d584cbb
Show file tree
Hide file tree
Showing 10 changed files with 267 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -30,5 +30,7 @@ public class UserModel : IUser, IUserRoles
public IList<string> Roles { get { return new List<string> { "administrator" }; } }

#endregion Implementation of IUserRoles

public bool CreatePersistentCookie { get; set; }
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@
@Scripts.Render("~/bundles/jqueryvalidate", "~/bundles/jqueryform")
<script src="~/Themes/TheAdmin/lib/iCheck/icheck.min.js"></script>
<script type="text/javascript">
var container = $(".box-info");
var form = container.find("form");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ public ActionResult SignIn(SignInViewModel model)

var user = _userService.GetUserByAccount(model.Account);

_authenticationService.SignIn(new UserModel { Identity = user.Id, UserName = user.Name }, model.Remember);
_authenticationService.SignIn(new UserModel { Identity = user.Id, UserName = user.Name, CreatePersistentCookie = model.Remember }, model.Remember);

if (string.IsNullOrWhiteSpace(model.ReturnUrl))
return RedirectToAction("Index", "Admin");
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
using Rabbit.Components.Security;
using Rabbit.Infrastructures.Mvc;
using Rabbit.Infrastructures.Security;
using Rabbit.Users.Services;
using Rabbit.Users.ViewModels;
using Rabbit.Web.Mvc.UI.Admin;
using System.Web.Mvc;

namespace Rabbit.Users.Controllers
{
[Admin]
public class UserAdminController : Controller
{
private readonly IAuthenticationService _authenticationService;
private readonly IUserService _userService;

public UserAdminController(IAuthenticationService authenticationService, IUserService userService)
{
_authenticationService = authenticationService;
_userService = userService;
}

public ActionResult Edit(string id)
{
var user = _userService.GetUserById(id ?? _authenticationService.GetAuthenticatedUser().Identity);
if (user == null)
return HttpNotFound();

var model = (UserEditViewModel)user;
return View(model);
}

[ValidateAntiForgeryToken, HttpPost]
public ActionResult Edit(UserEditViewModel model)
{
if (!ModelState.IsValid)
return this.Error("数据非法!");

var user = _userService.GetUserById(model.Id);
if (user == null)
return this.Error("数据非法!");

model.UpdateRecord(user);

var userModel = _authenticationService.GetAuthenticatedUser() as UserModel;
if (userModel != null && userModel.Identity == user.Id)
{
userModel.UserName = user.Name;
_authenticationService.SignIn(userModel, userModel.CreatePersistentCookie);
}

return this.Success();
}
}
}
16 changes: 16 additions & 0 deletions RabbitCMS/Web/Modules/Rabbit.Users/Rabbit.Users.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
<IISExpressWindowsAuthentication />
<IISExpressUseClassicPipelineMode />
<UseGlobalApplicationHostFile />
<WebGreaseLibPath>..\..\..\packages\WebGrease.1.5.2\lib</WebGreaseLibPath>
</PropertyGroup>
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Debug|AnyCPU' ">
<DebugSymbols>true</DebugSymbols>
Expand All @@ -39,6 +40,10 @@
<WarningLevel>4</WarningLevel>
</PropertyGroup>
<ItemGroup>
<Reference Include="Antlr3.Runtime, Version=3.4.1.9004, Culture=neutral, PublicKeyToken=eb42632606e9261f, processorArchitecture=MSIL">
<HintPath>..\..\..\packages\Antlr.3.4.1.9004\lib\Antlr3.Runtime.dll</HintPath>
<Private>True</Private>
</Reference>
<Reference Include="Autofac, Version=3.5.0.0, Culture=neutral, PublicKeyToken=17863af14b0044da, processorArchitecture=MSIL">
<HintPath>..\..\..\packages\Autofac.3.5.2\lib\net40\Autofac.dll</HintPath>
<Private>True</Private>
Expand Down Expand Up @@ -122,6 +127,10 @@
<HintPath>..\..\..\packages\Microsoft.AspNet.Mvc.5.2.3\lib\net45\System.Web.Mvc.dll</HintPath>
<Private>True</Private>
</Reference>
<Reference Include="System.Web.Optimization, Version=1.1.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35, processorArchitecture=MSIL">
<HintPath>..\..\..\packages\Microsoft.AspNet.Web.Optimization.1.1.3\lib\net40\System.Web.Optimization.dll</HintPath>
<Private>True</Private>
</Reference>
<Reference Include="System.Web.Razor, Version=3.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35, processorArchitecture=MSIL">
<HintPath>..\..\..\packages\Microsoft.AspNet.Razor.3.2.3\lib\net45\System.Web.Razor.dll</HintPath>
<Private>True</Private>
Expand All @@ -145,12 +154,17 @@
<Reference Include="System.Configuration" />
<Reference Include="System.Web.Services" />
<Reference Include="System.EnterpriseServices" />
<Reference Include="WebGrease, Version=1.5.2.14234, Culture=neutral, PublicKeyToken=31bf3856ad364e35, processorArchitecture=MSIL">
<HintPath>..\..\..\packages\WebGrease.1.5.2\lib\WebGrease.dll</HintPath>
<Private>True</Private>
</Reference>
</ItemGroup>
<ItemGroup>
<Content Include="packages.config">
<SubType>Designer</SubType>
</Content>
<Content Include="Views\Account\SignIn.cshtml" />
<Content Include="Views\UserAdmin\Edit.cshtml" />
<None Include="Web.Debug.config">
<DependentUpon>Web.config</DependentUpon>
</None>
Expand All @@ -166,6 +180,7 @@
<Compile Include="Adapter\FormsAuthenticationService.cs" />
<Compile Include="Adapter\RoleService.cs" />
<Compile Include="Controllers\AccountController.cs" />
<Compile Include="Controllers\UserAdminController.cs" />
<Compile Include="Migrators.cs" />
<Compile Include="Models\AccountRecord.cs" />
<Compile Include="Models\UserRecord.cs" />
Expand All @@ -174,6 +189,7 @@
<Compile Include="Services\AccountService.cs" />
<Compile Include="Services\UserService.cs" />
<Compile Include="ViewModels\AccountModels.cs" />
<Compile Include="ViewModels\UserViewModels.cs" />
</ItemGroup>
<ItemGroup>
<ProjectReference Include="..\..\..\Infrastructures\Rabbit.Infrastructures\Rabbit.Infrastructures.csproj">
Expand Down
44 changes: 44 additions & 0 deletions RabbitCMS/Web/Modules/Rabbit.Users/ViewModels/UserViewModels.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
using Rabbit.Users.Models;
using System.ComponentModel;
using System.ComponentModel.DataAnnotations;

namespace Rabbit.Users.ViewModels
{
public class UserEditViewModel
{
public string Id { get; set; }

[DisplayName("用户名称"), Required, StringLength(20)]
public string UserName { get; set; }

[DisplayName("账号名称"), Required, StringLength(20)]
public string AccountName { get; set; }

[DisplayName("密码"), StringLength(20, MinimumLength = 5)]
public string Password { get; set; }

[DisplayName("确认密码"), Compare("Password")]
public string ConfirmPassword { get; set; }

public static explicit operator UserEditViewModel(UserRecord record)
{
return new UserEditViewModel
{
Id = record.Id,
AccountName = record.Account.Account,
Password = record.Account.Password,
UserName = record.Name
};
}

public UserRecord UpdateRecord(UserRecord record)
{
record.Name = UserName;
record.Account.Account = AccountName;
if (!string.IsNullOrEmpty(Password))
record.Account.SetPassword(Password);

return record;
}
}
}
140 changes: 140 additions & 0 deletions RabbitCMS/Web/Modules/Rabbit.Users/Views/UserAdmin/Edit.cshtml
Original file line number Diff line number Diff line change
@@ -0,0 +1,140 @@
@using System.Web.Optimization
@model Rabbit.Users.ViewModels.UserEditViewModel
@helper PageStyles()
{
<style type="text/css">
.field-validation-error {
color: #dd4b39;
}
</style>
<link href="~/Themes/TheAdmin/lib/iCheck/all.css" rel="stylesheet" />
}
@helper PageScripts()
{
@Scripts.Render("~/bundles/jqueryvalidate", "~/bundles/jqueryform")
<script src="~/Themes/TheAdmin/lib/iCheck/icheck.min.js"></script>
<script type="text/javascript">
var container = $(".box-info");
var form = container.find("form");
var dialog=$(".alert-dismissable");
form.ajaxForm({
beforeSerialize: function () {
},
beforeSubmit: function (array, f) {
return f.valid();
},
success: function (data) {
dialog.removeClass("alert-danger", "alert-success");
if (data.success) {
dialog.addClass("alert-success");
dialog.find("span").html("保存成功!");
} else {
dialog.addClass("alert-danger");
dialog.find("span").html("保存失败,"+data.message);
}
dialog.fadeIn(function() {
setTimeout(function() {
dialog.fadeOut();
}, 1500);
});
},
error: function () {
dialog.removeClass("alert-danger", "alert-success").addClass("alert-danger");
dialog.find("span").html("保存失败,请联系管理员!");
dialog.fadeIn(function () {
setTimeout(function () {
dialog.fadeOut();
}, 1500);
});
}
});
</script>
}
@{
Layout.Styles = PageStyles();
Layout.Scripts = PageScripts();
var title = "编辑用户 - " + Model.UserName;
Layout.Title = title;
}

<div class="alert alert-danger alert-dismissable" style="display: none">
<button type="button" class="close" data-dismiss="alert" aria-hidden="true">×</button>
<h4><i class="icon fa fa-ban"></i> <span>删除失败,请联系管理员!</span></h4>
</div>
<div class="box box-info">
<div class="box-header with-border">
<h3 class="box-title">@title</h3>
</div><!-- /.box-header -->
<!-- form start -->
<form method="POST" class="form-horizontal">
@Html.HiddenFor(model=>model.Id)
@Html.AntiForgeryToken()
<div class="box-body">
<div class="form-group">
<label class="col-sm-2 control-label">@Html.DisplayNameFor(model => model.UserName)</label>
<div class="col-sm-10">
<div class="col-sm-8">
@Html.TextBoxFor(model => model.UserName, new {@class = "form-control"})
</div>
<div class="col-sm-2">
@Html.ValidationMessageFor(model => model.UserName)
</div>
</div>
</div>
<div class="form-group">
<label class="col-sm-2 control-label">@Html.DisplayNameFor(model => model.AccountName)</label>
<div class="col-sm-10">
<div class="col-sm-8">
@Html.TextBoxFor(model => model.AccountName, new {@class = "form-control"})
</div>
<div class="col-sm-2">
@Html.ValidationMessageFor(model => model.AccountName)
</div>
</div>
</div>
<div class="box box-warning collapsed-box">
<div class="box-header with-border">
<h3 class="box-title" data-widget="collapse" style="cursor: pointer">
修改密码
<small>(留空则不修改)</small>
</h3>
<div class="box-tools pull-right">
<button class="btn btn-box-tool" data-widget="collapse" title="折叠"><i class="fa fa-plus"></i></button>
</div>
</div>
<div class="box-body">
<div class="form-horizontal">
<div class="form-group">
<label class="col-sm-2 control-label">@Html.DisplayNameFor(model => model.Password)</label>
<div class="col-sm-10">
<div class="col-sm-8">
@Html.PasswordFor(model => model.Password, new {@class = "form-control"})
</div>
<div class="col-sm-2">
@Html.ValidationMessageFor(model => model.Password)
</div>
</div>
</div>
<div class="form-group">
<label class="col-sm-2 control-label">@Html.DisplayNameFor(model => model.ConfirmPassword)</label>
<div class="col-sm-10">
<div class="col-sm-8">
@Html.PasswordFor(model => model.ConfirmPassword, new {@class = "form-control"})
</div>
<div class="col-sm-2">
@Html.ValidationMessageFor(model => model.ConfirmPassword)
</div>
</div>
</div>
</div>
</div><!-- /.box-body -->
</div>
</div><!-- /.box-body -->
<div class="box-footer" style="text-align: center">
<button type="submit" class="btn btn-info">保存</button>
<button type="submit" class="btn btn-default">取消</button>
</div><!-- /.box-footer -->
</form>
</div>
2 changes: 1 addition & 1 deletion RabbitCMS/Web/Modules/Rabbit.Users/Web.config
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@
</dependentAssembly>
<dependentAssembly>
<assemblyIdentity name="WebGrease" publicKeyToken="31bf3856ad364e35" culture="neutral" />
<bindingRedirect oldVersion="0.0.0.0-1.6.5135.21930" newVersion="1.6.5135.21930" />
<bindingRedirect oldVersion="0.0.0.0-1.5.2.14234" newVersion="1.5.2.14234" />
</dependentAssembly>
<dependentAssembly>
<assemblyIdentity name="Antlr3.Runtime" publicKeyToken="eb42632606e9261f" culture="neutral" />
Expand Down
4 changes: 4 additions & 0 deletions RabbitCMS/Web/Modules/Rabbit.Users/packages.config
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
<?xml version="1.0" encoding="utf-8"?>
<packages>
<package id="Antlr" version="3.4.1.9004" targetFramework="net45" />
<package id="Autofac" version="3.5.2" targetFramework="net45" />
<package id="Castle.Core" version="3.3.3" targetFramework="net45" />
<package id="FluentMigrator" version="1.6.0" targetFramework="net45" />
Expand All @@ -8,6 +9,8 @@
<package id="Microsoft.AspNet.Mvc.zh-Hans" version="5.2.3" targetFramework="net45" />
<package id="Microsoft.AspNet.Razor" version="3.2.3" targetFramework="net45" />
<package id="Microsoft.AspNet.Razor.zh-Hans" version="3.2.3" targetFramework="net45" />
<package id="Microsoft.AspNet.Web.Optimization" version="1.1.3" targetFramework="net45" />
<package id="Microsoft.AspNet.Web.Optimization.zh-Hans" version="1.1.3" targetFramework="net45" />
<package id="Microsoft.AspNet.WebApi.Client" version="5.2.3" targetFramework="net45" />
<package id="Microsoft.AspNet.WebApi.Core" version="5.2.3" targetFramework="net45" />
<package id="Microsoft.AspNet.WebApi.WebHost" version="5.2.3" targetFramework="net45" />
Expand All @@ -21,4 +24,5 @@
<package id="Rabbit.Kernel" version="1.0.1.7" targetFramework="net45" />
<package id="Rabbit.Web" version="1.0.1.4" targetFramework="net45" />
<package id="Rabbit.Web.Mvc" version="1.0.1.7" targetFramework="net45" />
<package id="WebGrease" version="1.5.2" targetFramework="net45" />
</packages>
5 changes: 4 additions & 1 deletion RabbitCMS/Web/Themes/TheAdmin/Views/Layout.cshtml
Original file line number Diff line number Diff line change
Expand Up @@ -56,8 +56,11 @@
</li>
<!-- Menu Footer-->
<li class="user-footer">
<div class="pull-left">
<a href="@Url.Action("Edit","UserAdmin",new {area="Rabbit.Users"})" class="btn btn-default btn-flat">个人信息</a>
</div>
<div class="pull-right">
<a href="@Url.Action("SignOut","Account",new {area="Rabbit.Resources"})" class="btn btn-default btn-flat">登出</a>
<a href="@Url.Action("SignOut", "Account", new {area = "Rabbit.Users"})" class="btn btn-default btn-flat">退出</a>
</div>
</li>
</ul>
Expand Down

0 comments on commit d584cbb

Please sign in to comment.