The mood is philosophical and lyrical, so instead of working, I was drawn to reflect on how correctly the system of rights was implemented in CMS Bitrix and how I would do it.
To begin with about his worldview on the rights of users in information systems. Starting to feel my own, I hope, with the analytical department of the brain, I involuntarily have the desire to properly rationalize it and sort it out on the shelves (something that some lecturers lacked in lectures so much!).
This painful process begins with the allocation of a minimum set of independent entities. What are the entities or, in other words, how many data types?
')
Three data types: “object”, “operation on object”, “right to operation”.
An object can be any source of information in the system. For example, in a regular social network, this could be a user profile, a user's photo album, a photo in a photo album, a user’s blog, a community blog (the word “group” is already taken in a different sense), comments, after all, etc. Between objects can build up a hierarchical relationship. Let objects be divided into active, which can initiate interaction with other objects, and passive ones, which cannot do anything like this, but simply wait for someone to want to interact with them.
In the simple case, only the user account can be attributed to active objects, although the system can be built in such a way that the community, as a separate object, will initiate interaction with other objects, even if the chain was started by the same user.
For example, a user, as a community owner, wants to publish news on behalf of the community. Then the community can initiate interaction with the “news blog” object, the owner of which is “administrator”. The user can be called a natural person, and the community is legal. Hello lawyers. What and how to turn out is not the point here, but it is important to understand that an active object can be any object of the system with sufficient perversion of the creator of the system.
But in our case, let it be only the user, or rather his account, as an information unit.
Operations ... Oh ... Yes ... I mean, OOP ... Yes ... Lovers of this paradigm are already feeling. These are actions that can be performed on an object. Different objects have a different set of operations. For example, you can watch a photo album, edit your privacy settings, add photos, comment on them.
We used to live in paradise with bushes, that's all over. But we live on earth with m ****.
The right, or the right to an operation, is data that only active objects can possess. This data allows you to perform operations on objects.
But, unlike the first two objects, the “right of operation” is computed, dynamic data. There is also such a thing for them as “effective rights” to an object. Different systems calculate them differently. For example, they take your rights granted by the administrator + combine with the rights of the groups you are in, + your rights to the objects higher in the hierarchy, + the rights of the groups you are in, to objects higher in the hierarchy, + they take away the rights that the owner This object is set to prohibit for you and the groups in which you are a member, and voila, more precisely ... fuuh. And if the rights are not set for any of the listed objects, then the default rights are prepared for you. Go to Bitrix. That is, how should the API look for such a system?
$perms = GetPerms(active_object_id, passive_object_id, $operation=false);
if(perms['super_mega_permission'])
{
echo 'ALL';
}
else if(perms['write'])
{
echo 'only write :(';
}
etc
{
}
But in Bitrix a bunch of disparate and heterogeneous API for all occasions from Aunt Nyura. Not, in fact, they are the same, but the developers clearly do not use Okaka’s razor. I understand when for each module you need to set permissions by default, when users need to be distributed into different groups in the admin panel. Set a default group for registered users, etc. But why such a tricky API?
You will say not to make the data structure too complex. But I give a tooth, each of you will come up with the optimal structure. Maybe it is in Bitrix like this, but why then such an API? If you also take into account that Bitrix can not be set permissions for an individual user, but only for groups, then they can be cached nicely.
In conclusion, I will give examples of the API:
string CBlog::GetBlogUserCommentPerms( int ID, int userID );
string CBlog::GetBlogUserPostPerms ( int ID, int userID );
string GetBlogUserPostPerms::GetBlogUserCommentPerms( int ID, int userID );
string GetBlogUserPostPerms::GetBlogUserPostPerms ( int ID, int userID );
bool CSocNetUserPerms::CanPerformOperation( int fromUserID, int toUserID, string operation, bool bCurrentUserIsAdmin = false );
array CSocNetUserPerms::InitUserPerms( int currentUserID, int userID, bool bCurrentUserIsAdmin );
mixed CSocNetFeaturesPerms::CanPerformOperation( int userID, char type, mixed id, string feature, string operation, bool bUserIsAdmin = false );
bool CSocNetFeaturesPerms::CurrentUserCanPerformOperation( char type, int id, string feature, string operation );
CForumNew::GetUserPermission ().
I do not know if I am far from the limit, but I think that and so on.