AreWe

Untitled

Nov 18th, 2018
186
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 10.22 KB | None | 0 0
  1. Защо ми е User? Те всички регистрирани потребители са user...
  2.  
  3.  
  4. Admin123!@#
  5.  
  6. Легенда please????
  7.  
  8.  
  9.  
  10. //controller
  11. using System;
  12. using System.Collections.Generic;
  13. using System.Linq;
  14. using System.Web;
  15. using System.Web.Mvc;
  16. using MvcRoles.Models;//add
  17. using Microsoft.AspNet.Identity.EntityFramework; //add
  18. namespace MvcRoles.Controllers
  19. {
  20. public class RolesController : Controller
  21. {
  22. private ApplicationDbContext context = new ApplicationDbContext();
  23. // GET: Roles
  24. public ActionResult Index()
  25. {
  26. return View(context.Roles.ToList());
  27. }
  28.  
  29. public ActionResult Create()
  30. {
  31.  
  32.  
  33. return View();
  34. }
  35. [HttpPost]
  36. public ActionResult Create(FormCollection collection)
  37. {
  38. try
  39. {
  40. context.Roles.Add(new IdentityRole()
  41. {
  42. Name = collection["RoleName"]
  43. });
  44. context.SaveChanges();
  45. ViewBag.ResultMessage = "Role created successfully !";
  46. return View("Create");
  47. }
  48. catch
  49. {
  50. return View();
  51. }
  52. }
  53.  
  54. public ActionResult Delete(string RoleName)
  55. {
  56. var thisRole = context.Roles.Where(r => r.Name.Equals(RoleName, StringComparison.CurrentCultureIgnoreCase)).FirstOrDefault();
  57. context.Roles.Remove(thisRole);
  58. context.SaveChanges();
  59. return RedirectToAction("Create");
  60. }
  61.  
  62. //
  63. // GET: /Roles/Edit/5
  64. public ActionResult Edit(string roleName)
  65. {
  66. var thisRole = context.Roles.Where(r => r.Name.Equals(roleName, StringComparison.CurrentCultureIgnoreCase)).FirstOrDefault();
  67.  
  68. return View(thisRole);
  69. }
  70.  
  71. //
  72. // POST: /Roles/Edit/5
  73. [HttpPost]
  74. [ValidateAntiForgeryToken]
  75. public ActionResult Edit(Microsoft.AspNet.Identity.EntityFramework.IdentityRole role)
  76. {
  77. try
  78. {
  79. context.Entry(role).State = System.Data.Entity.EntityState.Modified;
  80. context.SaveChanges();
  81.  
  82. return RedirectToAction("Index");
  83. }
  84. catch
  85. {
  86. return View();
  87. }
  88. }
  89.  
  90. public ActionResult ManageUserRoles()
  91. {
  92. // prepopulat roles for the view dropdown
  93. var list = context.Roles.OrderBy(r => r.Name).ToList().Select(rr =>
  94.  
  95. new SelectListItem { Value = rr.Name.ToString(), Text = rr.Name }).ToList();
  96. ViewBag.Roles = list;
  97. return View("ManageUsers");
  98. }
  99.  
  100.  
  101. public ActionResult ManageUsers()
  102. {
  103. // prepopulat roles for the view dropdown
  104. var list = context.Roles.OrderBy(r => r.Name).ToList().Select(rr =>
  105. new SelectListItem { Value = rr.Name.ToString(), Text = rr.Name }).ToList();
  106. ViewBag.Roles = list;
  107. return View();
  108. }
  109.  
  110. [HttpPost]
  111. [ValidateAntiForgeryToken]
  112. public ActionResult RoleAddToUser(string UserName, string RoleName)
  113. {
  114. ApplicationUser user = context.Users.Where(u => u.UserName.Equals(UserName, StringComparison.CurrentCultureIgnoreCase)).FirstOrDefault();
  115. var manager = new ApplicationUserManager(new UserStore<ApplicationUser>(context));
  116. manager.AddToRole(user.Id, RoleName);
  117.  
  118. //ViewBag.ResultMessage = "Role created successfully !";
  119.  
  120. // prepopulat roles for the view dropdown
  121. var list = context.Roles.OrderBy(r => r.Name).ToList().Select(rr => new SelectListItem { Value = rr.Name.ToString(), Text = rr.Name }).ToList();
  122. ViewBag.Roles = list;
  123.  
  124. return View("ManageUsers");
  125. }
  126. [HttpPost]
  127. [ValidateAntiForgeryToken]
  128. public ActionResult GetRoles(string UserName)
  129. {
  130. if (!string.IsNullOrWhiteSpace(UserName))
  131. {
  132. ApplicationUser user = context.Users.Where(u => u.UserName.Equals(UserName, StringComparison.CurrentCultureIgnoreCase)).FirstOrDefault();
  133. var manager = new ApplicationUserManager(new UserStore<ApplicationUser>(context));
  134.  
  135. ViewBag.RolesForThisUser = manager.GetRoles(user.Id);
  136.  
  137. // prepopulat roles for the view dropdown
  138. var list = context.Roles.OrderBy(r => r.Name).ToList().Select(rr => new SelectListItem { Value = rr.Name.ToString(), Text = rr.Name }).ToList();
  139. ViewBag.Roles = list;
  140. }
  141.  
  142. return View("ManageUsers");
  143. }
  144. [HttpPost]
  145. [ValidateAntiForgeryToken]
  146. public ActionResult DeleteRoleForUser(string UserName, string RoleName)
  147. {
  148. var manager = new ApplicationUserManager(new UserStore<ApplicationUser>(context));
  149.  
  150. ApplicationUser user = context.Users.Where(u => u.UserName.Equals(UserName, StringComparison.CurrentCultureIgnoreCase)).FirstOrDefault();
  151.  
  152. if (manager.IsInRole(user.Id, RoleName))
  153. {
  154. manager.RemoveFromRole(user.Id, RoleName);
  155. ViewBag.ResultMessage = "Role removed from this user successfully !";
  156. }
  157. else
  158. {
  159. ViewBag.ResultMessage = "This user doesn't belong to selected role.";
  160. }
  161. // prepopulat roles for the view dropdown
  162. var list = context.Roles.OrderBy(r => r.Name).ToList().Select(rr => new SelectListItem { Value = rr.Name.ToString(), Text = rr.Name }).ToList();
  163. ViewBag.Roles = list;
  164.  
  165. return View("ManageUsers");
  166. }
  167. }
  168. }
  169.  
  170.  
  171.  
  172.  
  173. //index
  174. @model IEnumerable<Microsoft.AspNet.Identity.EntityFramework.IdentityRole>
  175. @{
  176. ViewBag.Title = "Index";
  177. }
  178.  
  179. <h2>Roles Listing </h2>
  180.  
  181. @Html.ActionLink("Create New Role", "Create") | @Html.ActionLink("Manage User Role", "ManageUserRoles")
  182. <hr />
  183. <div>
  184. @foreach (var role in Model)
  185. {
  186. <p>
  187. <strong>@role.Name | </strong>
  188. <span onclick="return confirm('Are you sure to delete?')"><a href="/Roles/[email protected]" class="delLink" style="color:red;">Delete</a></span> |
  189. @Html.ActionLink("Edit", "Edit", new { roleName = @role.Name })
  190. </p>
  191. }
  192. </div>
  193.  
  194.  
  195.  
  196. //create view
  197. @{
  198. ViewBag.Title = "Create";
  199. }
  200.  
  201.  
  202. <h2>Create Role</h2>
  203. @Html.ActionLink("List Roles", "Index") | @Html.ActionLink("Manage User Role", "ManageUserRoles")
  204. <hr />
  205. @using (Html.BeginForm())
  206. {
  207. @Html.AntiForgeryToken()
  208. @Html.ValidationSummary(true)
  209.  
  210. <div>
  211. Role name
  212. </div>
  213. <p>
  214. @Html.TextBox("RoleName")
  215. </p>
  216. <input type="submit" value="Save" />
  217. }
  218. @ViewBag.ResultMessage
  219.  
  220.  
  221. //edit view
  222. @model Microsoft.AspNet.Identity.EntityFramework.IdentityRole
  223. @{
  224. ViewBag.Title = "Edit";
  225. }
  226.  
  227. <h2>Edit Role</h2>
  228.  
  229. @Html.ActionLink("List Roles", "Index") | @Html.ActionLink("Manage User Role", "ManageUserRoles")
  230. <hr />
  231. @using (Html.BeginForm())
  232. {
  233. @Html.AntiForgeryToken()
  234. @Html.ValidationSummary(true)
  235. @Html.HiddenFor(model => model.Id)
  236. <div>
  237. Role name
  238. </div>
  239. <p>
  240. @Html.TextBoxFor(model => model.Name)
  241. </p>
  242. <input type="submit" value="Save" />
  243. }
  244.  
  245.  
  246.  
  247.  
  248.  
  249. //manage page
  250. @{
  251. ViewBag.Title = "ManageUserRoles";
  252. }
  253.  
  254. <h2>Manage User Roles</h2>
  255. @Html.ActionLink("Create New Role", "Create") | @Html.ActionLink("Manage User Role", "ManageUserRoles")
  256. <hr/>
  257.  
  258. <h2>Role Add to User</h2>
  259.  
  260. @using (Html.BeginForm("RoleAddToUser", "Roles"))
  261. {
  262. @Html.AntiForgeryToken()
  263. @Html.ValidationSummary(true)
  264.  
  265. <p>
  266. Username : @Html.TextBox("UserName")
  267. Role Name: @Html.DropDownList("RoleName", (IEnumerable <SelectListItem>) ViewBag.Roles, "Select ...")
  268.  
  269. </p>
  270.  
  271. <input type="submit" value="Save" />
  272. }
  273. <hr/>
  274. <h3>Get Roles for a User</h3>
  275. @using (Html.BeginForm("GetRoles", "Roles"))
  276. {
  277. @Html.AntiForgeryToken()
  278. <p>
  279. Username : @Html.TextBox("UserName")
  280. <input type="submit" value="Get Roles for this User" />
  281. </p>
  282. }
  283.  
  284. @if (ViewBag.RolesForThisUser != null)
  285. {
  286. <div style="background-color:yellow;">
  287. <h3>Roles for this user </h3>
  288. <ol>
  289. @foreach (string s in ViewBag.RolesForThisUser)
  290. {
  291. <li>@s</li>
  292. }
  293. </ol>
  294. </div>
  295. }
  296.  
  297. <hr />
  298. <h3>Delete A User from a Role</h3>
  299.  
  300. @using (Html.BeginForm("DeleteRoleForUser", "Roles"))
  301. {
  302. @Html.AntiForgeryToken()
  303. @Html.ValidationSummary(true)
  304.  
  305. <p>
  306. Username : @Html.TextBox("UserName")
  307. Role Name: @Html.DropDownList("RoleName", (IEnumerable<SelectListItem>)ViewBag.Roles, "Select ...")
  308.  
  309. </p>
  310.  
  311. <input type="submit" value="Delete this user from Role" />
  312. }
  313.  
  314.  
  315. //////////////////////
  316.  
  317. [Key]
  318. public int GenreId { get; set; }
  319.  
  320. [Display(Name = "Genre")]
  321. [Required]
  322. [StringLength(100, MinimumLength = 1)]
  323. public string GenretName { get; set; }
  324.  
  325. public virtual ICollection<Book> Books { get; set; }
  326.  
  327.  
  328.  
  329.  
  330.  
  331.  
  332. [Key]
  333. public int WriterId { get; set; }
  334.  
  335. [Display(Name = "First Name")]
  336. //[Required]
  337. [StringLength(200, MinimumLength = 1)]
  338. public string FirstName { get; set; }
  339.  
  340. [Display(Name = "Last Name")]
  341. //[Required]
  342. [StringLength(200, MinimumLength = 1)]
  343. public string LastName { get; set; }
  344.  
  345. [Display(Name = "Username")]
  346. //[Required]
  347. //[StringLength(150, MinimumLength = 1)]
  348. public string UserName { get; set; }
  349.  
  350. public virtual ICollection<Book> Books { get; set; }
  351.  
  352.  
  353.  
  354.  
  355.  
  356. [Key]
  357. public int BookId { get; set; }
  358.  
  359. [Required]
  360. [StringLength(300, MinimumLength = 1)]
  361. public string Title { get; set; }
  362.  
  363. [Display(Name = "Release date")]
  364. [DataType(DataType.Date)]
  365. public DateTime? ReleaseDate { get; set; }
  366.  
  367. [Required]
  368. [Display(Name = "Writer")]
  369. public int? WriterId { get; set; }
  370. public virtual Writer Writer { get; set; }
  371.  
  372. [Required]
  373. [Display(Name = "Genre")]
  374. public int? GenreId { get; set; }
  375. public virtual Genre Genre { get; set; }
  376.  
  377. [StringLength(500, MinimumLength = 1)]
  378. [Display(Name = "Description")]
  379. public string Description { get; set; }
Add Comment
Please, Sign In to add comment