Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using Microsoft.AspNet.Identity.EntityFramework;
- using System.Collections.Generic;
- using System.Linq;
- using System.Threading.Tasks;
- using Tariffs.Services.Common;
- namespace Tariffs.Services.Interfaces
- {
- public interface IRolesService : IService
- {
- IQueryable<string> GetAllRoleNames();
- Task<IList<string>> GetRolesForUserAsync(string userName);
- Task<IdentityRole> GetRoleByNameAsync(string roleName);
- Task UpdateRoleAsync(IdentityRole role);
- Task<bool> IsRoleUsedByUsersAsync(string roleName);
- Task<bool> AddRoleAsync(string roleName);
- Task<bool> AddUserToRoleAsync(string userName, string roleName);
- Task<bool> RemoveRoleAsync(string roleName);
- Task<bool> RemoveRoleFromUserAsync(string userName, string roleName);
- }
- }
- using System.Threading.Tasks;
- using Tariffs.Data.Models;
- using Tariffs.Services.Common;
- namespace Tariffs.Services.Interfaces
- {
- public interface IUsersService : IService
- {
- Task<User> GetUserByUserNameAsync(string userName);
- Task<string> GetUserIdByUserNameAsync(string userName);
- }
- }
- using Microsoft.AspNet.Identity;
- using Microsoft.AspNet.Identity.EntityFramework;
- using System;
- using System.Collections.Generic;
- using System.Data.Entity;
- using System.Linq;
- using System.Threading.Tasks;
- using Tariffs.Data;
- using Tariffs.Data.Models;
- using Tariffs.Services.Interfaces;
- namespace Tariffs.Services
- {
- public class RolesService : IRolesService
- {
- private TariffsDbContext dbContext;
- private RoleStore<IdentityRole> roleStore;
- private RoleManager<IdentityRole> roleManager;
- private UserStore<User> userStore;
- private UserManager<User> userManager;
- private IUsersService usersService;
- public RolesService(TariffsDbContext dbContext, IUsersService usersService)
- {
- this.dbContext = dbContext;
- this.usersService = usersService;
- roleStore = new RoleStore<IdentityRole>(dbContext);
- roleManager = new RoleManager<IdentityRole>(roleStore);
- userStore = new UserStore<User>(dbContext);
- userManager = new UserManager<User>(userStore);
- }
- public IQueryable<string> GetAllRoleNames()
- {
- IQueryable<string> rolesNames = dbContext.Roles.Select(r => r.Name);
- return rolesNames;
- }
- public async Task<IList<string>> GetRolesForUserAsync(string userName)
- {
- if (string.IsNullOrWhiteSpace(userName))
- {
- throw new ArgumentNullException("userName");
- }
- string userId = await usersService.GetUserIdByUserNameAsync(userName);
- if (string.IsNullOrWhiteSpace(userId))
- {
- throw new ArgumentNullException("user does not exists");
- }
- IList<string> rolesNames = await userManager.GetRolesAsync(userId);
- return rolesNames;
- }
- public async Task<IdentityRole> GetRoleByNameAsync(string roleName)
- {
- if (string.IsNullOrWhiteSpace(roleName))
- {
- throw new ArgumentNullException("roleName");
- }
- IdentityRole role = await roleManager.FindByNameAsync(roleName);
- return role;
- }
- public async Task UpdateRoleAsync(IdentityRole role)
- {
- if (role == null)
- {
- throw new ArgumentNullException("role");
- }
- var entry = dbContext.Entry(role);
- if (entry.State == EntityState.Detached)
- {
- this.dbContext.Roles.Attach(role);
- }
- entry.State = EntityState.Modified;
- await dbContext.SaveChangesAsync();
- }
- public async Task<bool> IsRoleUsedByUsersAsync(string roleName)
- {
- if (string.IsNullOrWhiteSpace(roleName))
- {
- throw new ArgumentNullException("roleName");
- }
- int usersCountWithThatRole = await dbContext.Roles
- .Where(r => r.Name == roleName)
- .Select(s => s.Users.Count())
- .FirstOrDefaultAsync();
- if (usersCountWithThatRole > 0)
- {
- return true;
- }
- return false;
- }
- public async Task<bool> AddRoleAsync(string roleName)
- {
- if (string.IsNullOrWhiteSpace(roleName))
- {
- throw new ArgumentNullException("roleName");
- }
- if (await DoesRoleExistsAsync(roleName) == false)
- {
- var role = new IdentityRole()
- {
- Name = roleName
- };
- var createdRoleResult = await roleManager.CreateAsync(role);
- if (createdRoleResult.Succeeded)
- {
- return true;
- }
- }
- return false;
- }
- public async Task<bool> AddUserToRoleAsync(string userName, string roleName)
- {
- if (string.IsNullOrWhiteSpace(userName))
- {
- throw new ArgumentNullException("userName");
- }
- if (string.IsNullOrWhiteSpace(roleName))
- {
- throw new ArgumentNullException("roleName");
- }
- string userId = await usersService.GetUserIdByUserNameAsync(userName);
- if (string.IsNullOrWhiteSpace(userId))
- {
- return false;
- }
- if (await DoesRoleExistsAsync(roleName) == false)
- {
- return false;
- }
- var result = await userManager.AddToRoleAsync(userId, roleName);
- if (result.Succeeded)
- {
- return true;
- }
- return false;
- }
- public async Task<bool> RemoveRoleFromUserAsync(string userName, string roleName)
- {
- if (string.IsNullOrWhiteSpace(userName))
- {
- throw new ArgumentNullException("userName");
- }
- if (string.IsNullOrWhiteSpace(roleName))
- {
- throw new ArgumentNullException("roleName");
- }
- string userId = await usersService.GetUserIdByUserNameAsync(userName);
- if (string.IsNullOrWhiteSpace(userId))
- {
- return false;
- }
- if (await userManager.IsInRoleAsync(userId, roleName))
- {
- IdentityResult result = await userManager.RemoveFromRoleAsync(userId, roleName);
- if (result.Succeeded)
- {
- return true;
- }
- }
- return false;
- }
- public async Task<bool> RemoveRoleAsync(string roleName)
- {
- if (string.IsNullOrWhiteSpace(roleName))
- {
- throw new ArgumentNullException("roleName");
- }
- // include user
- IdentityRole role = await dbContext.Roles.Where(r => r.Name == roleName).FirstOrDefaultAsync();
- if (role != null)
- {
- if (role.Users.Count > 0)
- {
- return false;
- }
- dbContext.Roles.Remove(role);
- await dbContext.SaveChangesAsync();
- return true;
- }
- return false;
- }
- private async Task<bool> DoesRoleExistsAsync(string roleName)
- {
- if (await roleManager.RoleExistsAsync(roleName))
- {
- return true;
- }
- return false;
- }
- }
- }
- using System;
- using System.Data.Entity;
- using System.Linq;
- using System.Threading.Tasks;
- using Tariffs.Data.Common.Repositories;
- using Tariffs.Data.Models;
- using Tariffs.Services.Interfaces;
- namespace Tariffs.Services
- {
- public class UsersService : IUsersService
- {
- private IRepository<User> users;
- public UsersService(IRepository<User> users)
- {
- this.users = users;
- }
- public async Task<User> GetUserByUserNameAsync(string userName)
- {
- if (string.IsNullOrWhiteSpace(userName))
- {
- throw new ArgumentNullException("userName");
- }
- User user = await users.All()
- .Where(u => u.UserName == userName)
- .FirstOrDefaultAsync();
- return user;
- }
- public async Task<string> GetUserIdByUserNameAsync(string userName)
- {
- if (string.IsNullOrWhiteSpace(userName))
- {
- throw new ArgumentNullException("userName");
- }
- string userId = await users.All().Where(u => u.UserName == userName).Select(u => u.Id).FirstOrDefaultAsync();
- return userId;
- }
- }
- }
Add Comment
Please, Sign In to add comment