Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Switch to logged_in_as(roles) to simplify auth logic! #5259

Closed
jywarren opened this issue Mar 25, 2019 · 6 comments · Fixed by #5660
Closed

Switch to logged_in_as(roles) to simplify auth logic! #5259

jywarren opened this issue Mar 25, 2019 · 6 comments · Fixed by #5660
Labels
fto-candidate issues which are meant to be solved by first timers but aren't well-formatted yet help wanted requires help by anyone willing to contribute multiple-use Ruby

Comments

@jywarren
Copy link
Member

the User model could use some simpler helpers, which would dramatically simplify some template code for showing things only to moderators or admins, like this:

<% if current_user && (current_user.role == "admin" || current_user.role == "moderator")%>
<h2><%= t('users.list.user_moderation') %></h2>
<p><%= t('users.list.admins_ban_spam') %> </p>
<% end %>

Instead, let's make a user model method like:

user.is_role(:admin)

Then that code could become:

if user.is_role(:admin, :moderator)

But we need to be able to specify if it's AND or OR -- any ideas?

Maybe:

user.is_one_of_roles(:admin, :moderator)

We could also simplify by making an application_helper method like:

user_has_role(:admin, :moderator)

where we also check if there is a current_user, further simplifying the code!

We'd love some help with this, and ideally the new methods would be tested as well! Thanks!

@jywarren jywarren added help wanted requires help by anyone willing to contribute Ruby labels Mar 25, 2019
@jywarren
Copy link
Member Author

Reposting from part of #268

@jywarren
Copy link
Member Author

Ack! We actually have this! So this issue could be for switching to this system:

def logged_in_as(roles)
if current_user
has_valid_role = false
roles.each do |role|
has_valid_role = true if current_user.role == role
end
has_valid_role
else
false
end
end

@jywarren
Copy link
Member Author

This could be broken into many first-timers-only issues using search results from this:

https://github.com/publiclab/plots2/search?q=%22current_user.role+%3D%3D%22&unscoped_q=%22current_user.role+%3D%3D%22


This has been marked as a good candidate for becoming a first-timers-only issue like these, meaning that it's simple, self-contained, and with some extra formatting, could be a great entry point for a new contributor. If you're familiar enough with this code, please consider reformatting or reposting it as a first-timers-only issue, and then ping @publiclab/reviewers to get it labelled. Or, if this is not your first time, try to solve it yourself!

@jywarren jywarren added the fto-candidate issues which are meant to be solved by first timers but aren't well-formatted yet label Mar 29, 2019
@jywarren jywarren changed the title Build user.is_role(:admin) to simplify auth logic! Switch to user.logged_in_as(roles) to simplify auth logic! Mar 29, 2019
@jywarren jywarren changed the title Switch to user.logged_in_as(roles) to simplify auth logic! Switch to logged_in_as(roles) to simplify auth logic! Mar 29, 2019
@jywarren
Copy link
Member Author

Made #5306 for admin_controller.rb!

@SrinandanPai
Copy link
Contributor

@jywarren I am a ROR developer and I am new to open source. I would like to work on this.

@SidharthBansal
Copy link
Member

SidharthBansal commented Apr 19, 2019 via email

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
fto-candidate issues which are meant to be solved by first timers but aren't well-formatted yet help wanted requires help by anyone willing to contribute multiple-use Ruby
Projects
None yet
3 participants