Advertisement
Guest User

Untitled

a guest
Jan 23rd, 2018
86
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 5.11 KB | None | 0 0
  1. using AteraanGuilds.DAL.Entities;
  2. using AteraanGuilds.Models.AccountViewModels;
  3. using AteraanGuilds.Service.Common;
  4. using Microsoft.AspNetCore.Identity;
  5. using Microsoft.AspNetCore.Mvc;
  6. using Microsoft.AspNetCore.Mvc.Rendering;
  7. using Microsoft.EntityFrameworkCore;
  8. using System;
  9. using System.Collections.Generic;
  10. using System.Linq;
  11. using System.Security.Claims;
  12. using System.Threading.Tasks;
  13.  
  14. namespace AteraanGuilds.Controllers
  15. {
  16.     public class UserController : Controller
  17.     {
  18.         private readonly UserManager<ApplicationUser> _userManager;
  19.         private readonly RoleManager<IdentityRole> _roleManager;
  20.         private readonly SignInManager<ApplicationUser> _signInManager;
  21.         protected IGuildService GuildService;
  22.  
  23.         public UserController(UserManager<ApplicationUser> userManager, RoleManager<IdentityRole> roleManager, SignInManager<ApplicationUser> signInManager, IGuildService guildService)
  24.         {
  25.             _userManager = userManager;
  26.             _roleManager = roleManager;
  27.             _signInManager = signInManager;
  28.             GuildService = guildService;
  29.         }
  30.  
  31.         [TempData]
  32.         public string ErrorMessage { get; set; }
  33.  
  34.         public async Task<ActionResult> Index()
  35.         {
  36.             if(_signInManager.IsSignedIn(User))
  37.             {
  38.                 var userList = _userManager.Users;
  39.                 var currentUserName = User.Identity.Name.ToString();
  40.                 var currentUser = new ApplicationUser();
  41.                 currentUser = await _userManager.FindByNameAsync(currentUserName);
  42.                 var guildUserList = await userList.Where(u => u.GuildId == currentUser.GuildId).ToListAsync();
  43.                 return View(guildUserList);
  44.             }
  45.  
  46.             return View(await _userManager.Users.ToListAsync());
  47.             //return View(guildUserList);
  48.         }
  49.  
  50.         public async Task<ActionResult> Create()
  51.         {
  52.            
  53.             ViewBag.GuildId = new SelectList(await GuildService.GetAsync(), "Id", "Name").OrderBy(x => x.Text);
  54.             //ViewBag.RoleId = new SelectList(await _roleManager.Roles.ToListAsync(), "Id", "Name").OrderBy(x => x.Text);
  55.             RegisterViewModel model = new RegisterViewModel();
  56.             List<SelectListItem> claimList = new List<SelectListItem>();
  57.  
  58.  
  59.             if (!_userManager.Users.Any())
  60.             {
  61.                 claimList.Add(new SelectListItem() { Text = "isAdmin", Value = "true" });
  62.                 claimList.Add(new SelectListItem() { Text = "CanAddGuilds", Value = "true" });
  63.                 claimList.Add(new SelectListItem() { Text = "CanRemoveGuilds", Value = "true" });
  64.                 claimList.Add(new SelectListItem() { Text = "CanEditGuilds", Value = "true" });
  65.             }
  66.             else
  67.             {
  68.  
  69.                 if(User.HasClaim("isAdmin","true"))
  70.                 {
  71.                     claimList.Add(new SelectListItem() { Text = "isAdmin", Value = "true" });
  72.                 }
  73.                 if(User.HasClaim("canAddGuilds", "true"))
  74.                 {
  75.                     claimList.Add(new SelectListItem() { Text = "CanAddGuilds", Value = "true" });
  76.                 }
  77.                 if(User.HasClaim("canRemoveGuilds", "true"))
  78.                 {
  79.                     claimList.Add(new SelectListItem() { Text = "CanRemoveGuilds", Value = "true" });
  80.                 }
  81.                 if(User.HasClaim("canEditGuilds", "true"))
  82.                 {
  83.                     claimList.Add(new SelectListItem() { Text = "CanEditGuilds", Value = "true" });
  84.                 }
  85.             }
  86.  
  87.             model.Claims = claimList;
  88.  
  89.             return View(model);
  90.         }
  91.  
  92.         [HttpPost]
  93.         public async Task<ActionResult> Create(RegisterViewModel model)
  94.         {
  95.             if (ModelState.IsValid)
  96.             {
  97.                 var user = new ApplicationUser { UserName = model.UserName, Email = model.Email, GuildId = model.GuildId};
  98.                 var result = await _userManager.CreateAsync(user, model.Password);
  99.                 foreach (var item in model.Claims)
  100.                 {
  101.                     if (item.Selected)
  102.                     {
  103.                         await _userManager.AddClaimAsync(user, new Claim(item.Text, item.Value));
  104.                     }
  105.                 }
  106.                 return RedirectToAction("Index");
  107.             }
  108.             return RedirectToAction("Index");
  109.         }
  110.  
  111.         public async Task<ActionResult> Delete(string id)
  112.         {
  113.             if (id == null)
  114.             {
  115.                 return RedirectToAction("Index");
  116.             }
  117.             var user = await _userManager.FindByIdAsync(id);
  118.             if (user == null)
  119.             {
  120.                 return RedirectToAction("Index");
  121.             }
  122.             return View(user);
  123.         }
  124.  
  125.         [HttpPost, ActionName("Delete")]
  126.         [ValidateAntiForgeryToken]
  127.         public async Task<ActionResult> DeleteConfirmed(string id)
  128.         {
  129.             var user = await _userManager.FindByIdAsync(id);
  130.             await _userManager.DeleteAsync(user);
  131.             return RedirectToAction("Index");
  132.         }
  133.  
  134.  
  135.  
  136.  
  137.  
  138.     }
  139. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement