Description
Hello,
I'm looking at adding a permissions system to my app and I am looking at attaching some data to a state which defines the permissions a user must have in order to access a state.
Example definition:
$stateProvider.state('admin', {
url: '/admin/:id',
templateUrl: '/admin.html',
controller: 'AdminCtrl',
data: {
auth: ['Staff', 'Admin'] // Could be a callback which is passed the state params
}
});
This data could be a set of roles which a user is required to have before they can access the state, or it could potentially contain a callback which would ideally have access to the parameters passed to a state to determine access to a state on a more fine grained (parameter determined) level.
This seems like it will work fine to restrict access to certain states. I can write a $stateChangeStart
authorization plugin which will check the next state, and redirect the user to a login or permission denied page if they don't have access, based on the data I defined on my state.
So far this is all possible. The problem I am trying to figure out is how to only display certain elements on a page such as navigation links based on these same permission tests.
I am thinking of writing a directive that would retrieve a state and run the same permission tests as above, showing or hiding the target element. At first, for the case where I want to show or hide navigation elements, I wanted to retrieve a state given a url, but haven't found a way to do this yet. Then I saw the ui-sref
directive, it would make just as much sense to create a directive similar to this, or modify this directive but also retrieve the state by name, run my authorization logic (passing any parameters through), and show or hide the element depending on result.
How can I retrieve a state by name? As far as I can tell, there are no accessors for this. Is there another way to accomplish this?
Thanks for your time.
TLDR
How do I retrieve a state by name?