Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using AteraanGuilds.DAL.Entities;
- using AteraanGuilds.Models.AccountViewModels;
- using AteraanGuilds.Service.Common;
- using Microsoft.AspNetCore.Identity;
- using Microsoft.AspNetCore.Mvc;
- using Microsoft.AspNetCore.Mvc.Rendering;
- using Microsoft.EntityFrameworkCore;
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Security.Claims;
- using System.Threading.Tasks;
- namespace AteraanGuilds.Controllers
- {
- public class UserController : Controller
- {
- private readonly UserManager<ApplicationUser> _userManager;
- private readonly RoleManager<IdentityRole> _roleManager;
- private readonly SignInManager<ApplicationUser> _signInManager;
- protected IGuildService GuildService;
- public UserController(UserManager<ApplicationUser> userManager, RoleManager<IdentityRole> roleManager, SignInManager<ApplicationUser> signInManager, IGuildService guildService)
- {
- _userManager = userManager;
- _roleManager = roleManager;
- _signInManager = signInManager;
- GuildService = guildService;
- }
- [TempData]
- public string ErrorMessage { get; set; }
- public async Task<ActionResult> Index()
- {
- if(_signInManager.IsSignedIn(User))
- {
- var userList = _userManager.Users;
- var currentUserName = User.Identity.Name.ToString();
- var currentUser = new ApplicationUser();
- currentUser = await _userManager.FindByNameAsync(currentUserName);
- var guildUserList = await userList.Where(u => u.GuildId == currentUser.GuildId).ToListAsync();
- return View(guildUserList);
- }
- return View(await _userManager.Users.ToListAsync());
- //return View(guildUserList);
- }
- public async Task<ActionResult> Create()
- {
- ViewBag.GuildId = new SelectList(await GuildService.GetAsync(), "Id", "Name").OrderBy(x => x.Text);
- //ViewBag.RoleId = new SelectList(await _roleManager.Roles.ToListAsync(), "Id", "Name").OrderBy(x => x.Text);
- RegisterViewModel model = new RegisterViewModel();
- List<SelectListItem> claimList = new List<SelectListItem>();
- if (!_userManager.Users.Any())
- {
- claimList.Add(new SelectListItem() { Text = "isAdmin", Value = "true" });
- claimList.Add(new SelectListItem() { Text = "CanAddGuilds", Value = "true" });
- claimList.Add(new SelectListItem() { Text = "CanRemoveGuilds", Value = "true" });
- claimList.Add(new SelectListItem() { Text = "CanEditGuilds", Value = "true" });
- }
- else
- {
- if(User.HasClaim("isAdmin","true"))
- {
- claimList.Add(new SelectListItem() { Text = "isAdmin", Value = "true" });
- }
- if(User.HasClaim("canAddGuilds", "true"))
- {
- claimList.Add(new SelectListItem() { Text = "CanAddGuilds", Value = "true" });
- }
- if(User.HasClaim("canRemoveGuilds", "true"))
- {
- claimList.Add(new SelectListItem() { Text = "CanRemoveGuilds", Value = "true" });
- }
- if(User.HasClaim("canEditGuilds", "true"))
- {
- claimList.Add(new SelectListItem() { Text = "CanEditGuilds", Value = "true" });
- }
- }
- model.Claims = claimList;
- return View(model);
- }
- [HttpPost]
- public async Task<ActionResult> Create(RegisterViewModel model)
- {
- if (ModelState.IsValid)
- {
- var user = new ApplicationUser { UserName = model.UserName, Email = model.Email, GuildId = model.GuildId};
- var result = await _userManager.CreateAsync(user, model.Password);
- foreach (var item in model.Claims)
- {
- if (item.Selected)
- {
- await _userManager.AddClaimAsync(user, new Claim(item.Text, item.Value));
- }
- }
- return RedirectToAction("Index");
- }
- return RedirectToAction("Index");
- }
- public async Task<ActionResult> Delete(string id)
- {
- if (id == null)
- {
- return RedirectToAction("Index");
- }
- var user = await _userManager.FindByIdAsync(id);
- if (user == null)
- {
- return RedirectToAction("Index");
- }
- return View(user);
- }
- [HttpPost, ActionName("Delete")]
- [ValidateAntiForgeryToken]
- public async Task<ActionResult> DeleteConfirmed(string id)
- {
- var user = await _userManager.FindByIdAsync(id);
- await _userManager.DeleteAsync(user);
- return RedirectToAction("Index");
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement