Advertisement
Guest User

Untitled

a guest
Oct 6th, 2015
145
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 9.57 KB | None | 0 0
  1. @model List<MySolution.Data.DAL.ApplicationUser>
  2. @{
  3. ViewBag.Title = Resources.Users;
  4. }
  5.  
  6. <h2>@Resources.Users</h2>
  7. <hr />
  8. <table class="table table-striped table-hover ">
  9. <thead>
  10. <tr>
  11. <th>@Resources.User</th>
  12. <th>@Resources.Roles</th>
  13. <th></th>
  14. <th></th>
  15. </tr>
  16. </thead>
  17. <tbody>
  18. @foreach (var user in Model)
  19. {
  20. <tr>
  21. <td>
  22. @user.UserName
  23. </td>
  24. <td>
  25. @user.DisplayRoles()
  26. </td>
  27. <td>
  28. @using (Html.BeginForm("UserAssignRole", "Admin", new { ReturnUrl = ViewBag.ReturnUrl }, FormMethod.Get, new { @class = "form-horizontal", role = "form" }))
  29. {
  30. @Html.AntiForgeryToken()
  31. @Html.HiddenFor(m => m.Where(u => u.Id.Equals(user.Id)).FirstOrDefault().UserName)
  32. <input type="submit" value="@Resources.AssignRole" class="btn btn-default btn-sm" />
  33. }
  34. </td>
  35. <td>
  36. @using (Html.BeginForm("UserRemoveRole", "Admin", new { ReturnUrl = ViewBag.ReturnUrl }, FormMethod.Get, new { @class = "form-horizontal", role = "form" }))
  37. {
  38. @Html.AntiForgeryToken()
  39. @Html.HiddenFor(m => m.Where(u => u.Id.Equals(user.Id)).FirstOrDefault().UserName)
  40. <input type="submit" value="@Resources.RemoveRole" class="btn btn-default btn-sm" />
  41. }
  42. </td>
  43. </tr>
  44. }
  45. </tbody>
  46. </table>
  47.  
  48. //
  49. // GET: /Admin/UserIndex
  50. [Authorize(Roles = "Admin")]
  51. public ActionResult UserIndex()
  52. {
  53. var users = context.Users.ToList();
  54. return View(users);
  55. }
  56.  
  57. //
  58. // GET: /Admin/UserAssignRole
  59. [HttpGet]
  60. //[ValidateAntiForgeryToken]
  61. public ActionResult UserAssignRole(UserAssignRoleViewModel vm)
  62. {
  63. ViewBag.Username = vm.Username;
  64. ViewBag.Roles = context.Roles.OrderBy(r => r.Name).ToList().Select(rr => new SelectListItem { Value = rr.Name.ToString(), Text = rr.Name }).ToList();
  65.  
  66. return View("UserAssignRole");
  67. }
  68.  
  69. //
  70. // POST: /Admin/UserAssignRole
  71. [HttpPost]
  72. //[ValidateAntiForgeryToken]
  73. [ActionName("UserAssignRole")]
  74. public ActionResult UserAssignRolePost(UserAssignRoleViewModel vm)
  75. {
  76. ApplicationUser user = context.Users.Where(u => u.UserName.Equals(vm.Username, StringComparison.CurrentCultureIgnoreCase)).FirstOrDefault();
  77. this.UserManager.AddToRole(user.Id, vm.Role);
  78.  
  79. return RedirectToAction("UserIndex");
  80. }
  81.  
  82. /// <summary>
  83. /// ViewsAdminUserAssignRole.cshtml
  84. /// </summary>
  85. public class UserAssignRoleViewModel
  86. {
  87. [Display(Name = "Username", ResourceType = typeof(Resources))]
  88. public string Username { get; set; }
  89.  
  90. [Display(Name = "Role", ResourceType = typeof(Resources))]
  91. public string Role { get; set; }
  92. }
  93.  
  94. @model UserAssignRoleViewModel
  95. @{
  96. ViewBag.Title = Resources.AssignRole;
  97. }
  98.  
  99. <h2>@Resources.AssignRole</h2>
  100. <hr />
  101. <div class="row">
  102. <div class="col-md-8">
  103. <section id="assignRoleForm">
  104. @using (Html.BeginForm("UserAssignRole", "Admin", new { ReturnUrl = ViewBag.ReturnUrl }, FormMethod.Post, new { @class = "form-horizontal", role = "form" }))
  105. {
  106. @Html.AntiForgeryToken()
  107. @Html.ValidationSummary(true, "", new { @class = "text-danger" })
  108.  
  109. <div class="form-group">
  110. @Html.LabelFor(m => m.Username, new { @class = "col-md-2 control-label" })
  111. <div class="col-md-10">
  112. @Html.TextBoxFor(m => m.Username, new { @class = "form-control" , @readonly = "readonly" })
  113. @Html.ValidationMessageFor(m => m.Username, "", new { @class = "text-danger" })
  114. </div>
  115. </div>
  116. <div class="form-group">
  117. @Html.LabelFor(m => m.Role, new { @class = "col-md-2 control-label" })
  118. <div class="col-md-10">
  119. @Html.DropDownListFor(m => m.Role, (IEnumerable<SelectListItem>)ViewBag.Roles, Resources.DropdownSelect, new { @class = "form-control" })
  120. </div>
  121. </div>
  122. <div class="form-group">
  123. <div class="col-md-offset-2 col-md-10">
  124. <input type="submit" value="@Resources.Assign" class="btn btn-default" />
  125. </div>
  126. </div>
  127. }
  128. </section>
  129. </div>
  130. </div>
  131.  
  132. @model IEnumerable<Microsoft.AspNet.Identity.EntityFramework.IdentityRole>
  133. @{
  134. ViewBag.Title = Resources.Roles;
  135. }
  136.  
  137. <h2>@Resources.Roles</h2>
  138. <hr />
  139. @using (Html.BeginForm("RoleCreate", "Admin", new { ReturnUrl = ViewBag.ReturnUrl }, FormMethod.Get, new { @class = "form-horizontal", role = "form" }))
  140. {
  141. <input type="submit" value="@Resources.CreateRole" class="btn btn-default btn-sm" />
  142. }
  143. <hr />
  144.  
  145. <table class="table table-striped table-hover ">
  146. <thead>
  147. <tr>
  148. <th>@Resources.Role</th>
  149. <th></th>
  150. <th></th>
  151. </tr>
  152. </thead>
  153. <tbody>
  154. @foreach (var role in Model)
  155. {
  156. <tr>
  157. <td>
  158. @role.Name
  159. </td>
  160. <td>
  161. @using (Html.BeginForm("RoleEdit", "Admin", new { ReturnUrl = ViewBag.ReturnUrl }, FormMethod.Get, new { @class = "form-horizontal", role = "form" }))
  162. {
  163. @Html.AntiForgeryToken()
  164. @Html.HiddenFor(m => m.Where(r => r.Id.Equals(role.Id)).FirstOrDefault().Name)
  165. <input type="submit" value="@Resources.Edit" class="btn btn-default btn-sm" />
  166. }
  167. </td>
  168. <td>
  169. <input type="submit" value="@Resources.Delete" class="btn btn-default btn-sm" data-toggle="modal" data-target="#confirm-delete"/>
  170.  
  171. <div class="modal fade" id="confirm-delete" tabindex="-1" role="dialog" aria-labelledby="myModalLabel" aria-hidden="true">
  172. <div class="modal-dialog">
  173. <div class="modal-content">
  174. <div class="modal-header">
  175. @Resources.DeleteRole
  176. </div>
  177. <div class="modal-body">
  178. @Resources.AreYouSureYouWantToDelete
  179. </div>
  180. <div class="modal-footer">
  181. @using (Html.BeginForm("RoleDelete", "Admin", new { ReturnUrl = ViewBag.ReturnUrl }, FormMethod.Post, new { @class = "form-horizontal", role = "form" }))
  182. {
  183. @Html.AntiForgeryToken()
  184. @Html.HiddenFor(m => m.Where(r => r.Id.Equals(role.Id)).FirstOrDefault().Name)
  185. <button type="button" class="btn btn-default" data-dismiss="modal">@Resources.Cancel</button>
  186. <input type="submit" value="@Resources.Delete" class="btn btn-danger btn-ok" />
  187. }
  188. </div>
  189. </div>
  190. </div>
  191. </div>
  192. </td>
  193. </tr>
  194. }
  195. </tbody>
  196. </table>
  197.  
  198. /// <summary>
  199. /// ViewsAdminRoleCreate.cshtml
  200. /// </summary>
  201. public class RoleCreateViewModel
  202. {
  203. [Required]
  204. [Display(Name = "Name", ResourceType = typeof(Resources))]
  205. public string Name { get; set; }
  206. }
  207.  
  208. //
  209. // GET: /Admin/RoleCreate
  210. [HttpGet]
  211. [Authorize(Roles = "Admin")]
  212. public ActionResult RoleCreate()
  213. {
  214. return View();
  215. }
  216.  
  217. //
  218. // POST: /Admin/RoleCreate
  219. [HttpPost]
  220. [Authorize(Roles = "Admin")]
  221. public ActionResult RoleCreate(RoleCreateViewModel vm)
  222. {
  223. context.Roles.Add(new IdentityRole()
  224. {
  225. Name = vm.Name
  226. });
  227. context.SaveChanges();
  228. ViewBag.ResultMessage = Resources.RoleCreatedSuccessfully;
  229. return RedirectToAction("RoleIndex");
  230. }
  231.  
  232. @model RoleCreateViewModel
  233. @{
  234. ViewBag.Title = Resources.CreateRole;
  235. }
  236.  
  237. <h2>@Resources.CreateRole</h2>
  238. <hr />
  239. <div class="row">
  240. <div class="col-md-8">
  241. <section id="createRoleForm">
  242. @using (Html.BeginForm("RoleCreate", "Admin", new { ReturnUrl = ViewBag.ReturnUrl }, FormMethod.Post, new { @class = "form-horizontal", role = "form" }))
  243. {
  244. @Html.AntiForgeryToken()
  245. @Html.ValidationSummary(true)
  246.  
  247. <div class="form-group">
  248. @Html.LabelFor(m => m.Name, new { @class = "col-md-2 control-label" })
  249. <div class="col-md-10">
  250. @Html.TextBoxFor(m => m.Name, new { @class = "form-control" })
  251. @Html.ValidationMessageFor(m => m.Name, "", new { @class = "text-danger" })
  252. </div>
  253. </div>
  254. <div class="form-group">
  255. <div class="col-md-offset-2 col-md-10">
  256. <input type="submit" value="@Resources.Save" class="btn btn-default" />
  257. </div>
  258. </div>
  259. }
  260. </section>
  261. </div>
  262. </div>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement