/// <summary> /// Adds the roles to the specified user if permission allows it /// </summary> /// <param name="performingUserId">User performing the role addition</param> /// <param name="adjustedUserId">User having roles added</param> /// <param name="rolesToAdd">Roles to be added to the user</param> /// <returns></returns> public IEnumerable <IRoleDto> AddRolesToUser(int performingUserId, int adjustedUserId, IEnumerable <IRoleDto> rolesToAdd) { using (var context = new PrometheusContext()) { List <Role> newRoles = new List <Role>(); foreach (var role in rolesToAdd) { newRoles.Add((from r in context.Roles where r.Id == role.Id select r).First()); /* attach context objects */ } if (!context.Users.Any(x => x.Id == adjustedUserId)) { throw new EntityNotFoundException("Could not add Roles to User.", typeof(User), adjustedUserId); } var updatedUser = context.Users.Find(adjustedUserId); updatedUser.Roles = new List <Role>(); context.Users.Attach(updatedUser); foreach (var role in newRoles) { updatedUser.Roles.Add(role); } context.Entry(updatedUser).State = EntityState.Modified; context.SaveChanges(); foreach (var updatedUserRole in updatedUser.Roles) { yield return(ManualMapper.MapRoleToDto(updatedUserRole)); } } }
/// <summary> /// Retrieve a single role /// </summary> /// <param name="performingUserId">user making hte request</param> /// <param name="roleId">role to retrieve</param> /// <returns></returns> public IRoleDto GetRole(int performingUserId, int roleId) { if (_permissionController.UserHasPermission(performingUserId, UserRoleAssignment.CanViewRoles)) { using (var context = new PrometheusContext()) { return(ManualMapper.MapRoleToDto(context.Roles.FirstOrDefault(r => r.Id == roleId))); } } return(null); }
protected override IRoleDto Create(int performingUserId, IRoleDto roleDto) { using (var context = new PrometheusContext()) { var role = context.Roles.Find(roleDto.Id); if (role != null) { throw new InvalidOperationException(string.Format("Role with ID {0} already exists.", roleDto.Id)); } var savedRole = context.Roles.Add(ManualMapper.MapDtoToRole(roleDto)); context.SaveChanges(performingUserId); return(ManualMapper.MapRoleToDto(savedRole)); } }
/// <summary> /// Get all available roles /// </summary> /// <param name="performingUserId">user requesting the action</param> /// <returns></returns> public IEnumerable <IRoleDto> GetRoles(int performingUserId) { if (_permissionController.UserHasPermission(performingUserId, UserRoleAssignment.CanViewRoles)) { using (var context = new PrometheusContext()) { var roles = context.Roles; foreach (var role in roles) { yield return(ManualMapper.MapRoleToDto(role)); } } } }
protected override IRoleDto Update(int performingUserId, IRoleDto roleDto) { using (var context = new PrometheusContext()) { if (!context.Roles.Any(x => x.Id == roleDto.Id)) { throw new InvalidOperationException(string.Format("Role with ID {0} cannot be updated since it does not exist.", roleDto.Id)); } var updatedRole = ManualMapper.MapDtoToRole(roleDto); context.Roles.Attach(updatedRole); context.Entry(updatedRole).State = EntityState.Modified; context.SaveChanges(performingUserId); return(ManualMapper.MapRoleToDto(updatedRole)); } }
/// <summary> /// Retrieves all of the Roles attributed the the User with the ID supplied /// </summary> /// <param name="userId"></param> /// <returns></returns> private IEnumerable <IRoleDto> GetUserRoles(int userId) { using (var context = new PrometheusContext()) { var user = context.Users.Find(userId); if (user == null) { throw new EntityNotFoundException("", typeof(User), userId); } foreach (var userRole in user.Roles) { yield return(ManualMapper.MapRoleToDto(userRole)); } } }