@@ -57,6 +57,18 @@ bool Enforcer :: AddRoleForUser(string user, string role) {
5757 return this ->AddGroupingPolicy (params);
5858}
5959
60+ // AddRolesForUser adds roles for a user.
61+ // Returns false if the user already has the roles (aka not affected).
62+ bool Enforcer :: AddRolesForUser(string user, vector<string> roles) {
63+ bool f = false ;
64+ for (int i=0 ;i<roles.size ();i++) {
65+ bool b = this ->AddGroupingPolicy ({user, roles[i]});
66+ if (b)
67+ f = true ;
68+ }
69+ return f;
70+ }
71+
6072// DeleteRoleForUser deletes a role for a user.
6173// Returns false if the user does not have the role (aka not affected).
6274bool Enforcer :: DeleteRoleForUser(string user, string role) {
@@ -210,24 +222,23 @@ vector<vector<string>> Enforcer :: GetImplicitPermissionsForUser(string user, ve
210222// GetImplicitUsersForPermission("data1", "read") will get: ["alice", "bob"].
211223// Note: only users will be returned, roles (2nd arg in "g") will be excluded.
212224vector<string> Enforcer :: GetImplicitUsersForPermission(vector<string> permission) {
213- vector<string> subjects = this ->GetAllSubjects ();
214- vector<string> roles = this ->GetAllRoles ();
225+ vector<string> p_subjects = this ->GetAllSubjects ();
226+ vector<string> g_inherit = this ->model ->GetValuesForFieldInPolicyAllTypes (" g" , 1 );
227+ vector<string> g_subjects = this ->model ->GetValuesForFieldInPolicyAllTypes (" g" , 0 );
215228
216- vector<string> users = SetSubtract (subjects, roles);
229+ vector<string> subjects (p_subjects);
230+ subjects.insert (subjects.end (), g_subjects.begin (), g_subjects.end ());
231+ ArrayRemoveDuplicates (subjects);
217232
218233 vector<string> res;
219- for (int i = 0 ; i < users.size () ; i++) {
220- Scope scope = InitializeScope ();
221- PushObject (scope);
222- PushStringPropToObject (scope, " r" , users[i], " sub" );
223- PushStringPropToObject (scope, " r" , permission[0 ], " obj" );
224- PushStringPropToObject (scope, " r" , permission[1 ], " act" );
225-
226- bool allowed = this ->Enforce (scope);
227-
228- if (allowed)
229- res.push_back (users[i]);
234+ for (int i=0 ;i<subjects.size ();i++) {
235+ bool allowed = this ->Enforce ({subjects[i], permission[0 ], permission[1 ]});
236+
237+ if (allowed) {
238+ res.push_back (subjects[i]);
239+ }
230240 }
231241
242+ res = SetSubtract (res, g_inherit);
232243 return res;
233244}
0 commit comments