/// <summary> /// Removes user from specified role /// </summary> /// <param name="user">User</param> /// <param name="roleName">Role name</param> /// <returns></returns> public Task RemoveFromRoleAsync(DalUser user, string roleName) { if (user == null) { throw new ArgumentNullException(nameof(user)); } if (string.IsNullOrEmpty(roleName)) { throw new ArgumentNullException(nameof(roleName)); } if (user.Roles.Count == 0) { return(Task.FromResult <object>(null)); } var roleStore = new DalRoleStore(context); DalRole dalRole = roleStore.FindByNameAsync(roleName).Result; if (dalRole != null) { if (user.Roles.Contains(dalRole)) { user.Roles.Remove(dalRole); this.UpdateAsync(user); } return(Task.FromResult <object>(null)); } else { throw new ArgumentOutOfRangeException(nameof(roleName)); } }
/// <summary> /// Add user to role /// </summary> /// <param name="user">User</param> /// <param name="roleName">Name of applying role</param> /// <returns></returns> public async Task AddToRoleAsync(DalUser user, string roleName) { if (user == null) { throw new ArgumentNullException(nameof(user)); } if (string.IsNullOrEmpty(roleName)) { throw new ArgumentNullException(nameof(roleName)); } var roleStore = new DalRoleStore(context); DalRole dalRole = await roleStore.FindByNameAsync(roleName); if (dalRole != null) { if (user.Roles == null) { user.Roles = new List <DalRole>(); } user.Roles.Clear(); user.Roles.Add(dalRole); await UpdateAsync(user); } }
/// <summary> /// Check if user is in specifyed role /// </summary> /// <param name="user">User to check</param> /// <param name="roleName">Name of role</param> /// <returns>true - if user in role</returns> public Task <bool> IsInRoleAsync(DalUser user, string roleName) { if (user == null) { throw new ArgumentNullException(nameof(user)); } if (string.IsNullOrEmpty(roleName)) { throw new ArgumentNullException(nameof(roleName)); } var roleStore = new DalRoleStore(context); DalRole dalRole = roleStore.FindByNameAsync(roleName).Result; if (dalRole != null) { if (user.Roles != null && user.Roles.Contains(dalRole)) { return(Task.FromResult <bool>(true)); } else { return(Task.FromResult <bool>(false)); } } else { throw new ArgumentOutOfRangeException(nameof(roleName)); } }