-
Notifications
You must be signed in to change notification settings - Fork 97
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Get List<Menu> based on user role or claim #30
Comments
Hey @fasteddys , |
Thanks for that @mo-esmp this is a great lib, very smart and intelligent. Since a user can have more than one roles, I was hoping for something just a little bit different, in the previous approach the entire menu is exposed even to unauthorized users, I understand they will be blocked when they access.
Is there a way to get just the Previously I used to do this... which is not really helpful, because I need get all users roles and then find the controller/actions.
|
You mean with |
Hello @mo-esmp, please correct me if I am wrong. Adding Feel free to checkout Breadcrumbs https://github.com/zHaytam/SmartBreadcrumbs Here is a some sample stuff I was trying to use, but the ASP Core MVC views does not have any good options. Let me also share with you that your lib. is so flexible, I can manage parts of view, for e.g. inside a dashboard I can manage widget access as well!! 👍 very cool [Generator]
public class MenuPagesGenerator : ISourceGenerator
{
private const string RouteAttributeName = "Microsoft.AspNetCore.Components.RouteAttribute";
private const string DescriptionAttributeName = "System.ComponentModel.Description";
public void Initialize(GeneratorInitializationContext context) { }
public void Execute(GeneratorExecutionContext context)
{
try
{
var menuComponents = GetMenuComponents(context.Compilation);
var pageDetailsSource = SourceText.From(Templates.MenuPages(menuComponents), Encoding.UTF8);
context.AddSource("PageDetails", pageDetailsSource);
context.AddSource("PageDetail", SourceText.From(Templates.PageDetail(), Encoding.UTF8));
}
catch (Exception)
{
Debugger.Launch();
}
}
private static ImmutableArray<RouteableComponent> GetMenuComponents(Compilation compilation)
{
// Get all classes
IEnumerable<SyntaxNode> allNodes = compilation.SyntaxTrees.SelectMany(s => s.GetRoot().DescendantNodes());
IEnumerable<ClassDeclarationSyntax> allClasses = allNodes
.Where(d => d.IsKind(SyntaxKind.ClassDeclaration))
.OfType<ClassDeclarationSyntax>();
return allClasses
.Select(component => TryGetMenuComponent(compilation, component))
.Where(page => page is not null)
.Cast<RouteableComponent>()// stops the nullable lies
.ToImmutableArray();
}
private static RouteableComponent? TryGetMenuComponent(Compilation compilation, ClassDeclarationSyntax component)
{
var attributes = component.AttributeLists
.SelectMany(x => x.Attributes)
.Where(attr =>
attr.Name.ToString() == RouteAttributeName
|| attr.Name.ToString() == DescriptionAttributeName)
.ToList();
var routeAttribute = attributes.FirstOrDefault(attr => attr.Name.ToString() == RouteAttributeName);
var descriptionAttribute = attributes.FirstOrDefault(attr => attr.Name.ToString() == DescriptionAttributeName);
if (routeAttribute is null || descriptionAttribute is null)
{
return null;
}
if (
routeAttribute.ArgumentList?.Arguments.Count != 1 ||
descriptionAttribute.ArgumentList?.Arguments.Count != 1)
{
// no route path or description value
return null;
}
var semanticModel = compilation.GetSemanticModel(component.SyntaxTree);
var routeArg = routeAttribute.ArgumentList.Arguments[0];
var routeExpr = routeArg.Expression;
var routeTemplate = semanticModel.GetConstantValue(routeExpr).ToString();
var descriptionArg = descriptionAttribute.ArgumentList.Arguments[0];
var descriptionExpr = descriptionArg.Expression;
var title = semanticModel.GetConstantValue(descriptionExpr).ToString();
return new RouteableComponent(routeTemplate, title);
}
} https://andrewlock.net/using-source-generators-to-generate-a-nav-component-in-a-blazor-app/ Currently.. similarl concept.. but I want to make this fully dynami, resuable so others can also use in the lib
|
@fasteddys Thanks for sharing the code and sorry for the late reply. I think we can divide this feature into 3 sections:
I can help with the implementation of 1 and 3 :) |
thanks @mo-esmp sure, I can test it, happy to see you open to new ideas and pull our code snippets into one consolidate idea!! 🤗 it |
Hello @mo-esmp bump +1 😄 |
Hey @fasteddys, I guess I can start implementation this weekend but before that do think IMvcControllerDiscovery and MvcControllerInfo can help in case of having list of controllers and actions to generate links? |
Sure, I trust your design approach, looks interesting. For our users, lets do that & allow our developers who add our
|
@fasteddys I'm a bit confused. To create the menu in a dynamic way, what are the requirements?
|
@mo-esmp not the expert here, but lets just start with something straight forward
To weave HTML, here's a couple of options either the template engine or pull it via a
|
Let's keep this open. It will take time but in the end, we will implement this feature. |
hi bump |
Hello, thanks for this, its very creative approach and unique, is it possible to build a menu hierarchy based on the users roles/claims at startup/cached, could you please show us so I can tie the
Mo-esmpMenuHelper
into a_SideBarLayout
I can help with adding the BS 5 styles with a PR if you want.
The text was updated successfully, but these errors were encountered: