public virtual ActionResult Get(int?id, ItemRange range, IEnumerable <SortColumn> sortColumns) { // If there is no id then return all the projects within the given range. if (!id.HasValue) { // Load all the items in order to apply sorting. var result = _projectRepository.List(0, int.MaxValue, out var totalCount); var projects = result.Select(_viewModelConverter.ToViewModel).ToList(); AddExtendedFields(projects); if (sortColumns != null) { projects = projects.AsQueryable().OrderBy(sortColumns).ToList(); } if (_projectOptions.ShowVisibleTo) { var currentUser = PrincipalInfo.CurrentPrincipal.Identity.Name; var currentUserRoles = _roleProvider.GetRolesForUser(currentUser).ToList(); projects = projects.Where(x => IsProjectAvailable(x, currentUser, currentUserRoles)).ToList(); } return(Rest(projects, range)); } // Otherwise get the project by id. var project = _projectRepository.Get(id.Value); if (project == null) { // Return a 404 if no project exists for the given id. return(new RestStatusCodeResult(HttpStatusCode.NotFound)); } var extendedProjectViewModel = _viewModelConverter.ToViewModel(project); AddExtendedFields(extendedProjectViewModel); if (_projectOptions.ShowVisibleTo) { var currentUser = PrincipalInfo.CurrentPrincipal.Identity.Name; var currentUserRoles = _roleProvider.GetRolesForUser(currentUser).ToList(); if (!IsProjectAvailable(extendedProjectViewModel, currentUser, currentUserRoles)) { return(new RestStatusCodeResult(HttpStatusCode.Forbidden)); } } return(Rest(extendedProjectViewModel)); }
/// <summary> /// Gets manual and synched roles for user /// </summary> /// <param name="userName"></param> /// <returns></returns> public override IEnumerable <string> GetRolesForUser(string userName) { var defaultRoles = new HashSet <string>(_synchedProvider.GetRolesForUser(userName)); var extendedRoles = _extendedRoleProvider.GetRolesForUser(userName); foreach (var role in extendedRoles) { defaultRoles.Add(role); } return(defaultRoles); }
public ActionResult Index() { bool created = false; var user = _userProvider.GetUser("*****@*****.**"); if (user == null) { var userCreated = _userProvider.CreateUser("*****@*****.**", "Episerver123!", "*****@*****.**", "Do you like cats?", "Who doesn't like cats?", isApproved: true, out UIUserCreateStatus status, out IEnumerable <string> errors); if (status != UIUserCreateStatus.Success) { return(Content($"Admin user ([email protected] / Episerver123!) failed to create [Error: {string.Join(",", errors)}]")); } created = true; user = userCreated; } // Ensure Role Exists var adminRole = _roleProvider.GetAllRoles(); if (!adminRole.Any(x => x.Name == AdminRole)) { _roleProvider.CreateRole(AdminRole); } // Ensure seed user in admin role var roles = _roleProvider.GetRolesForUser(user.Username); if (!roles.Contains(AdminRole)) { _roleProvider.AddUserToRoles(user.Username, new[] { AdminRole }); } return(Content($"Admin user ([email protected] / Episerver123!) exists already and is in {AdminRole} role")); }