Skip to content
This repository has been archived by the owner on Mar 18, 2021. It is now read-only.

Fix global role bug #58

Open
wants to merge 11 commits into
base: master
Choose a base branch
from
Open

Fix global role bug #58

wants to merge 11 commits into from

Conversation

pablorodriguez
Copy link

@pablorodriguez pablorodriguez commented Oct 25, 2018

Fix the code that checks for global roles
On this comment I explain what it the error that we have when we ask for global roles to a user
https://teamg5.atlassian.net/browse/FD-287?focusedCommentId=10492&page=com.atlassian.jira.plugin.system.issuetabpanels%3Acomment-tabpanel

@pablorodriguez pablorodriguez requested a review from maeve October 25, 2018 23:50
Copy link

@jcstringer jcstringer left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can we get some specs to go with this change?

@bbauer
Copy link

bbauer commented Oct 26, 2018

+1 on specs. Also, @maeve do you mind taking a look at this?

Copy link
Contributor

@maeve maeve left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm still waiting on Jira access, so I'm not sure of the exact bug we're trying to fix here. In the meantime, I did review the code, added a couple suggestions for tweaks, and pointed out what might potentially cause problems.

It definitely needs specs. Right now the existing tests are passing because admin? returns a superset of admin?(true), but we should have tests around both cases for each method that's been added or changed.

end
end

def admin?(global_scope=false)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm concerned about the side effects of changing the default behavior for a set of methods that are already used across multiple applications. It could be the right thing to do but it may have unforeseen consequences somewhere. Has there been an audit of the places where the admin?, editor? and viewer? methods are already being used?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

for those who are calling admin? editor?``viewer?without params it will have the same efect, check if there is any role with that name. Just those that call them with the params global_scope knows that they are traing to search the role but at global scope. In this case we add a new filter that is resource_type: nil
In other part of the code I found this
@current_user.roles.global.exists? where is global scope defined ?

@@ -92,7 +102,7 @@ def has_global_role?
end

def global_role?
super_admin? || admin? || editor? || viewer?
super_admin? || admin?(true) || editor?(true) || viewer?(true)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

As long as we're changing the implementation of this method, we can do this in one trip to the DB.

Suggested change
super_admin? || admin?(true) || editor?(true) || viewer?(true)
user.roles.global.exists?

def check_for_role(role_name,global_scope=false)
if user.present?
query = user.roles.where(name: role_name)
query = query.where(resource_type: nil) if global_scope
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The rolify gem provides a number of convenient scopes on the roles relation.

Suggested change
query = query.where(resource_type: nil) if global_scope
query = query.global if global_scope

user.present? && user.has_role?(:admin)
def check_for_role(role_name,global_scope=false)
if user.present?
query = user.roles.where(name: role_name)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If the global_scope argument is false, this query will return all roles with that role_name, including global roles. Is that the intention?

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants