Quote:
That doesn't mean you should do it that way. However, none of
your proposed approaches solves the problem you identified of having
anyone add or remove Authorities (which I'll also call Roles).
The EnumSet does solve that problem (I think).
Anyone with access to the database can of course grant or revoke
authorities from users, but not add or remove the Authority itself.
Quote:
They can add a new boolean columns, they can stuff new values
into your bit mapped or string fields, etc.
Yes, they could. But that would not change the Enum, there would be
additional data, or possibly broken data (in case of
stuff-it-all-into-one-field approaches).
But I think I get your point.
What bothers me the most with making Authorities full-fledged
hibernate-mapped objects is that I can now refer to the (well-known)
Authorities by identifiers like ROLE_USER in my code without having
to first load anything from the DB.
I haven't actually spelled it out in code yet and may be totally off-track.
Now with my Enum I can write:
if User.getAuthorities.contains(Authority.ROLE_USER) {
.. // grant access;
}
(I don't because in my toy-project I have also resorted to have the
security managed by Acegi Security, but that is a different matter).
If I did the same with a non-Enum Authority-class I would first have to
load the Authorities I know by name and store them somewhere. Possibly
in static fields of Authority. The syntax would then be the same.
I would have to make these static fields in Authority publicly writeable
because the class itself is Hibernate-agnostic of course and so
the instances loaded from the DB would have to be stuffed in from the
outside. Then I would need a check whether all that are needed are
still there.
Doable, of course. But not as elegant as the EnumSet. Or so I feel now.
Quote:
P.S. If you found this post helpful, please consider giving me a
credit by clicking "Yes" on the line below.
Yes, absolutly. Have done so.
Ciao, MM