SHOW:
|
|
- or go back to the newest paste.
| 1 | using Microsoft.AspNet.Identity.EntityFramework; | |
| 2 | using System.Collections.Generic; | |
| 3 | using System.Linq; | |
| 4 | using System.Threading.Tasks; | |
| 5 | using Tariffs.Services.Common; | |
| 6 | ||
| 7 | namespace Tariffs.Services.Interfaces | |
| 8 | {
| |
| 9 | public interface IRolesService : IService | |
| 10 | {
| |
| 11 | IQueryable<string> GetAllRoleNames(); | |
| 12 | ||
| 13 | Task<IList<string>> GetRolesForUserAsync(string userName); | |
| 14 | ||
| 15 | Task<IdentityRole> GetRoleByNameAsync(string roleName); | |
| 16 | ||
| 17 | Task UpdateRoleAsync(IdentityRole role); | |
| 18 | ||
| 19 | Task<bool> IsRoleUsedByUsersAsync(string roleName); | |
| 20 | ||
| 21 | Task<bool> AddRoleAsync(string roleName); | |
| 22 | ||
| 23 | Task<bool> AddUserToRoleAsync(string userName, string roleName); | |
| 24 | ||
| 25 | Task<bool> RemoveRoleAsync(string roleName); | |
| 26 | ||
| 27 | Task<bool> RemoveRoleFromUserAsync(string userName, string roleName); | |
| 28 | } | |
| 29 | } | |
| 30 | ||
| 31 | using System.Threading.Tasks; | |
| 32 | using Tariffs.Data.Models; | |
| 33 | using Tariffs.Services.Common; | |
| 34 | ||
| 35 | namespace Tariffs.Services.Interfaces | |
| 36 | {
| |
| 37 | public interface IUsersService : IService | |
| 38 | {
| |
| 39 | Task<User> GetUserByUserNameAsync(string userName); | |
| 40 | ||
| 41 | Task<string> GetUserIdByUserNameAsync(string userName); | |
| 42 | } | |
| 43 | } | |
| 44 | ||
| 45 | using Microsoft.AspNet.Identity; | |
| 46 | using Microsoft.AspNet.Identity.EntityFramework; | |
| 47 | using System; | |
| 48 | using System.Collections.Generic; | |
| 49 | using System.Data.Entity; | |
| 50 | using System.Linq; | |
| 51 | using System.Threading.Tasks; | |
| 52 | using Tariffs.Data; | |
| 53 | using Tariffs.Data.Models; | |
| 54 | using Tariffs.Services.Interfaces; | |
| 55 | ||
| 56 | namespace Tariffs.Services | |
| 57 | {
| |
| 58 | public class RolesService : IRolesService | |
| 59 | {
| |
| 60 | private TariffsDbContext dbContext; | |
| 61 | private RoleStore<IdentityRole> roleStore; | |
| 62 | private RoleManager<IdentityRole> roleManager; | |
| 63 | private UserStore<User> userStore; | |
| 64 | private UserManager<User> userManager; | |
| 65 | private IUsersService usersService; | |
| 66 | ||
| 67 | public RolesService(TariffsDbContext dbContext, IUsersService usersService) | |
| 68 | {
| |
| 69 | this.dbContext = dbContext; | |
| 70 | this.usersService = usersService; | |
| 71 | roleStore = new RoleStore<IdentityRole>(dbContext); | |
| 72 | roleManager = new RoleManager<IdentityRole>(roleStore); | |
| 73 | userStore = new UserStore<User>(dbContext); | |
| 74 | userManager = new UserManager<User>(userStore); | |
| 75 | } | |
| 76 | ||
| 77 | public IQueryable<string> GetAllRoleNames() | |
| 78 | {
| |
| 79 | IQueryable<string> rolesNames = dbContext.Roles.Select(r => r.Name); | |
| 80 | return rolesNames; | |
| 81 | } | |
| 82 | ||
| 83 | public async Task<IList<string>> GetRolesForUserAsync(string userName) | |
| 84 | {
| |
| 85 | if (string.IsNullOrWhiteSpace(userName)) | |
| 86 | {
| |
| 87 | throw new ArgumentNullException("userName");
| |
| 88 | } | |
| 89 | ||
| 90 | string userId = await usersService.GetUserIdByUserNameAsync(userName); | |
| 91 | ||
| 92 | if (string.IsNullOrWhiteSpace(userId)) | |
| 93 | {
| |
| 94 | throw new ArgumentNullException("user does not exists");
| |
| 95 | } | |
| 96 | ||
| 97 | IList<string> rolesNames = await userManager.GetRolesAsync(userId); | |
| 98 | ||
| 99 | return rolesNames; | |
| 100 | } | |
| 101 | ||
| 102 | public async Task<IdentityRole> GetRoleByNameAsync(string roleName) | |
| 103 | {
| |
| 104 | if (string.IsNullOrWhiteSpace(roleName)) | |
| 105 | {
| |
| 106 | throw new ArgumentNullException("roleName");
| |
| 107 | } | |
| 108 | ||
| 109 | IdentityRole role = await roleManager.FindByNameAsync(roleName); | |
| 110 | ||
| 111 | return role; | |
| 112 | } | |
| 113 | ||
| 114 | public async Task UpdateRoleAsync(IdentityRole role) | |
| 115 | {
| |
| 116 | if (role == null) | |
| 117 | {
| |
| 118 | throw new ArgumentNullException("role");
| |
| 119 | } | |
| 120 | ||
| 121 | var entry = dbContext.Entry(role); | |
| 122 | if (entry.State == EntityState.Detached) | |
| 123 | {
| |
| 124 | this.dbContext.Roles.Attach(role); | |
| 125 | } | |
| 126 | ||
| 127 | entry.State = EntityState.Modified; | |
| 128 | ||
| 129 | await dbContext.SaveChangesAsync(); | |
| 130 | } | |
| 131 | ||
| 132 | public async Task<bool> IsRoleUsedByUsersAsync(string roleName) | |
| 133 | {
| |
| 134 | if (string.IsNullOrWhiteSpace(roleName)) | |
| 135 | {
| |
| 136 | throw new ArgumentNullException("roleName");
| |
| 137 | } | |
| 138 | ||
| 139 | int usersCountWithThatRole = await dbContext.Roles | |
| 140 | .Where(r => r.Name == roleName) | |
| 141 | .Select(s => s.Users.Count()) | |
| 142 | .FirstOrDefaultAsync(); | |
| 143 | ||
| 144 | if (usersCountWithThatRole > 0) | |
| 145 | {
| |
| 146 | return true; | |
| 147 | } | |
| 148 | ||
| 149 | return false; | |
| 150 | } | |
| 151 | ||
| 152 | public async Task<bool> AddRoleAsync(string roleName) | |
| 153 | {
| |
| 154 | if (string.IsNullOrWhiteSpace(roleName)) | |
| 155 | {
| |
| 156 | throw new ArgumentNullException("roleName");
| |
| 157 | } | |
| 158 | ||
| 159 | if (await DoesRoleExistsAsync(roleName) == false) | |
| 160 | {
| |
| 161 | var role = new IdentityRole() | |
| 162 | {
| |
| 163 | Name = roleName | |
| 164 | }; | |
| 165 | ||
| 166 | var createdRoleResult = await roleManager.CreateAsync(role); | |
| 167 | ||
| 168 | if (createdRoleResult.Succeeded) | |
| 169 | {
| |
| 170 | return true; | |
| 171 | } | |
| 172 | } | |
| 173 | ||
| 174 | return false; | |
| 175 | } | |
| 176 | ||
| 177 | public async Task<bool> AddUserToRoleAsync(string userName, string roleName) | |
| 178 | {
| |
| 179 | if (string.IsNullOrWhiteSpace(userName)) | |
| 180 | {
| |
| 181 | throw new ArgumentNullException("userName");
| |
| 182 | } | |
| 183 | ||
| 184 | if (string.IsNullOrWhiteSpace(roleName)) | |
| 185 | {
| |
| 186 | throw new ArgumentNullException("roleName");
| |
| 187 | } | |
| 188 | ||
| 189 | ||
| 190 | string userId = await usersService.GetUserIdByUserNameAsync(userName); | |
| 191 | ||
| 192 | if (string.IsNullOrWhiteSpace(userId)) | |
| 193 | {
| |
| 194 | return false; | |
| 195 | } | |
| 196 | ||
| 197 | if (await DoesRoleExistsAsync(roleName) == false) | |
| 198 | {
| |
| 199 | return false; | |
| 200 | } | |
| 201 | ||
| 202 | var result = await userManager.AddToRoleAsync(userId, roleName); | |
| 203 | if (result.Succeeded) | |
| 204 | {
| |
| 205 | return true; | |
| 206 | } | |
| 207 | ||
| 208 | return false; | |
| 209 | } | |
| 210 | ||
| 211 | public async Task<bool> RemoveRoleFromUserAsync(string userName, string roleName) | |
| 212 | {
| |
| 213 | if (string.IsNullOrWhiteSpace(userName)) | |
| 214 | {
| |
| 215 | throw new ArgumentNullException("userName");
| |
| 216 | } | |
| 217 | ||
| 218 | if (string.IsNullOrWhiteSpace(roleName)) | |
| 219 | {
| |
| 220 | throw new ArgumentNullException("roleName");
| |
| 221 | } | |
| 222 | ||
| 223 | string userId = await usersService.GetUserIdByUserNameAsync(userName); | |
| 224 | ||
| 225 | if (string.IsNullOrWhiteSpace(userId)) | |
| 226 | {
| |
| 227 | return false; | |
| 228 | } | |
| 229 | ||
| 230 | if (await userManager.IsInRoleAsync(userId, roleName)) | |
| 231 | {
| |
| 232 | IdentityResult result = await userManager.RemoveFromRoleAsync(userId, roleName); | |
| 233 | if (result.Succeeded) | |
| 234 | {
| |
| 235 | return true; | |
| 236 | } | |
| 237 | } | |
| 238 | ||
| 239 | return false; | |
| 240 | } | |
| 241 | ||
| 242 | public async Task<bool> RemoveRoleAsync(string roleName) | |
| 243 | {
| |
| 244 | if (string.IsNullOrWhiteSpace(roleName)) | |
| 245 | {
| |
| 246 | throw new ArgumentNullException("roleName");
| |
| 247 | } | |
| 248 | ||
| 249 | // include user | |
| 250 | IdentityRole role = await dbContext.Roles.Where(r => r.Name == roleName).FirstOrDefaultAsync(); | |
| 251 | ||
| 252 | if (role != null) | |
| 253 | {
| |
| 254 | if (role.Users.Count > 0) | |
| 255 | {
| |
| 256 | return false; | |
| 257 | } | |
| 258 | ||
| 259 | dbContext.Roles.Remove(role); | |
| 260 | await dbContext.SaveChangesAsync(); | |
| 261 | ||
| 262 | return true; | |
| 263 | } | |
| 264 | ||
| 265 | return false; | |
| 266 | } | |
| 267 | ||
| 268 | private async Task<bool> DoesRoleExistsAsync(string roleName) | |
| 269 | {
| |
| 270 | if (await roleManager.RoleExistsAsync(roleName)) | |
| 271 | {
| |
| 272 | return true; | |
| 273 | } | |
| 274 | ||
| 275 | return false; | |
| 276 | } | |
| 277 | } | |
| 278 | } | |
| 279 | ||
| 280 | using System; | |
| 281 | using System.Data.Entity; | |
| 282 | using System.Linq; | |
| 283 | using System.Threading.Tasks; | |
| 284 | using Tariffs.Data.Common.Repositories; | |
| 285 | using Tariffs.Data.Models; | |
| 286 | using Tariffs.Services.Interfaces; | |
| 287 | ||
| 288 | namespace Tariffs.Services | |
| 289 | {
| |
| 290 | public class UsersService : IUsersService | |
| 291 | {
| |
| 292 | private IRepository<User> users; | |
| 293 | public UsersService(IRepository<User> users) | |
| 294 | {
| |
| 295 | this.users = users; | |
| 296 | } | |
| 297 | ||
| 298 | public async Task<User> GetUserByUserNameAsync(string userName) | |
| 299 | {
| |
| 300 | if (string.IsNullOrWhiteSpace(userName)) | |
| 301 | {
| |
| 302 | throw new ArgumentNullException("userName");
| |
| 303 | } | |
| 304 | ||
| 305 | User user = await users.All() | |
| 306 | .Where(u => u.UserName == userName) | |
| 307 | .FirstOrDefaultAsync(); | |
| 308 | ||
| 309 | return user; | |
| 310 | } | |
| 311 | ||
| 312 | public async Task<string> GetUserIdByUserNameAsync(string userName) | |
| 313 | {
| |
| 314 | if (string.IsNullOrWhiteSpace(userName)) | |
| 315 | {
| |
| 316 | throw new ArgumentNullException("userName");
| |
| 317 | } | |
| 318 | ||
| 319 | string userId = await users.All().Where(u => u.UserName == userName).Select(u => u.Id).FirstOrDefaultAsync(); | |
| 320 | ||
| 321 | return userId; | |
| 322 | } | |
| 323 | } | |
| 324 | } |