If a User has a set of roles attached to them and each role has a set of scopes, how can I check if the User has all the scopes
For example, I have a mapping of roles to scopes
scopes_for_roles = {
artist: ['draw', 'sing'],
teacher: ['read', 'write'],
athlete: ['draw', 'read', 'dance']
}
For User_A who has roles athlete and teacher, how can I check (return T/F) if he/she has the following scopes?
scopes = ['read', 'write'] --> TRUE
scopes = ['sing', 'read', 'write', 'dance'] --> FALSE # Missing sing
What I've tried is the below
roles.product(scopes).select { |role, scope| scopes_for_roles[role].include?(scope) if scopes_for_roles.key?(role) }.size >= scopes.size
However, I believe the snippet above has a bug where it only checks the .size of the selected permutations and not the actual scopes themselves. So if a User has the roles teacher and athlete, the snippet will return true for scopes ['read', 'sing'] because 'read' is found TWICE in roles teacher and athlete.
Note: I could have several for loops w/ key-value checks but I was wondering if there was a cleaner/shorter approach like the snippet I provided