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

Refactor admin_controller with simpler user login/role check #5306

Open
first-timers bot opened this issue Mar 29, 2019 · 3 comments

Comments

Projects
None yet
3 participants
@first-timers
Copy link

commented Mar 29, 2019

Hi, this is a first-timers-only issue. This means we've worked to make it more legible to folks who either haven't contributed to our codebase before, or even folks who haven't contributed to open source before.

If that's you, we're interested in helping you take the first step and can answer questions and help you out as you do. Note that we're especially interested in contributions from people from groups underrepresented in free and open source software!

We know that the process of creating a pull request is the biggest barrier for new contributors. This issue is for you 💝

If you have contributed before, consider leaving this one for someone new, and looking through our general help wanted issues. Thanks!

🤔 What you will need to know.

Nothing. This issue is meant to welcome you to Open Source :) We are happy to walk you through the process.

📋 Step by Step

  • 🙋 Claim this issue: Comment below. If someone else has claimed it, ask if they've opened a pull request already and if they're stuck -- maybe you can help them solve a problem or move it along!

  • 📝 Update the file app/controllers/admin_controller.rb in the plots2 repository (press the little pen Icon) and edit the line as shown below.

See this page for some help in taking your first steps!

Below is a "diff" showing in red (and a -) which lines to remove, and in green (and a +) which lines to add:

@@ -7,7 +7,7 @@ def assets; end
   def promote_admin
     @user = User.find params[:id]
     unless @user.nil?
-      if current_user && current_user.role == 'admin'
+      if logged_in_as(['admin'])
         @user.role = 'admin'
         @user.save
         flash[:notice] = "User '<a href='/profile/" + @user.username + "'>" + @user.username + "</a>' is now an admin."
@@ -21,7 +21,7 @@ def promote_admin
   def promote_moderator
     @user = User.find params[:id]
     unless @user.nil?
-      if current_user && (current_user.role == 'moderator' || current_user.role == 'admin')
+      if logged_in_as(['admin', 'moderator'])
         @user.role = 'moderator'
         @user.save
         flash[:notice] = "User '<a href='/profile/" + @user.username + "'>" + @user.username + "</a>' is now a moderator."
@@ -35,7 +35,7 @@ def promote_moderator
   def demote_basic
     @user = User.find params[:id]
     unless @user.nil?
-      if current_user && (current_user.role == 'moderator' || current_user.role == 'admin')
+      if logged_in_as(['admin', 'moderator'])
         @user.role = 'basic'
         @user.save
         flash[:notice] = "User '<a href='/profile/" + @user.username + "'>" + @user.username + "</a>' is no longer a moderator."
@@ -47,7 +47,7 @@ def demote_basic
   end
 
   def reset_user_password
-    if current_user && current_user.role == 'admin'
+    if logged_in_as(['admin'])
       user = User.find(params[:id])
       if user
         key = user.generate_reset_key
@@ -61,7 +61,7 @@ def reset_user_password
   end
 
   def useremail
-    if current_user && (current_user.role == 'moderator' || current_user.role == 'admin')
+    if logged_in_as(['admin', 'moderator'])
       if params[:address]
         # address was submitted. find the username(s) and return.
         @address = params[:address]
@@ -75,7 +75,7 @@ def useremail
   end
 
   def spam
-    if current_user && (current_user.role == 'moderator' || current_user.role == 'admin')
+    if logged_in_as(['admin', 'moderator'])
       @nodes = Node.paginate(page: params[:page])
                    .order('nid DESC')
       @nodes = if params[:type] == 'wiki'
@@ -90,7 +90,7 @@ def spam
   end
 
   def spam_revisions
-    if current_user && (current_user.role == 'moderator' || current_user.role == 'admin')
+    if logged_in_as(['admin', 'moderator'])
       @revisions = Revision.paginate(page: params[:page])
                            .order('timestamp DESC')
                            .where(status: 0)
@@ -115,7 +115,7 @@ def spam_comments
 
   def mark_spam
     @node = Node.find params[:id]
-    if current_user && (current_user.role == 'moderator' || current_user.role == 'admin')
+    if logged_in_as(['admin', 'moderator'])
       if @node.status == 1 || @node.status == 4
         @node.spam
         @node.author.ban
@@ -138,7 +138,7 @@ def mark_spam
 
   def mark_comment_spam
     @comment = Comment.find params[:id]
-    if current_user && (current_user.role == 'moderator' || current_user.role == 'admin')
+    if logged_in_as(['admin', 'moderator'])
       if @comment.status == 1 || @comment.status == 4
         @comment.spam
         user = @comment.author
@@ -155,7 +155,7 @@ def mark_comment_spam
   end
 
   def publish_comment
-    if current_user && (current_user.role == 'moderator' || current_user.role == 'admin')
+    if logged_in_as(['admin', 'moderator'])
       @comment = Comment.find params[:id]
       if @comment.status == 1
         flash[:notice] = 'Comment already published.'
@@ -181,7 +181,7 @@ def publish_comment
   end
 
   def publish
-    if current_user && (current_user.role == 'moderator' || current_user.role == 'admin')
+    if logged_in_as(['admin', 'moderator'])
       @node = Node.find params[:id]
       if @node.status == 1
         flash[:notice] = 'Item already published.'
@@ -223,7 +223,7 @@ def mark_spam_revision
       return
     end
 
-    if current_user && (current_user.role == 'moderator' || current_user.role == 'admin')
+    if logged_in_as(['admin', 'moderator'])
       if @revision.status == 1
         @revision.spam
         @revision.author.ban
@@ -244,7 +244,7 @@ def mark_spam_revision
   end
 
   def publish_revision
-    if current_user && (current_user.role == 'moderator' || current_user.role == 'admin')
+    if logged_in_as(['admin', 'moderator'])
       @revision = Revision.find params[:vid]
       @revision.publish
       @revision.author.unban
@@ -262,7 +262,7 @@ def publish_revision
 
   def moderate
     user = User.find params[:id]
-    if current_user && (current_user.role == 'moderator' || current_user.role == 'admin')
+    if logged_in_as(['admin', 'moderator'])
       user.moderate
       flash[:notice] = 'The user has been moderated.'
     else
@@ -273,7 +273,7 @@ def moderate
 
   def unmoderate
     user = User.find params[:id]
-    if current_user && (current_user.role == 'moderator' || current_user.role == 'admin')
+    if logged_in_as(['admin', 'moderator'])
       user.unmoderate
       flash[:notice] = 'The user has been unmoderated.'
     else
@@ -284,7 +284,7 @@ def unmoderate
 
   def ban
     user = User.find params[:id]
-    if current_user && (current_user.role == 'moderator' || current_user.role == 'admin')
+    if logged_in_as(['admin', 'moderator'])
       user.ban
       flash[:notice] = 'The user has been banned.'
     else
@@ -295,7 +295,7 @@ def ban
 
   def unban
     user = User.find params[:id]
-    if current_user && (current_user.role == 'moderator' || current_user.role == 'admin')
+    if logged_in_as(['admin', 'moderator'])
       user.unban
       flash[:notice] = 'The user has been unbanned.'
     else
@@ -305,7 +305,7 @@ def unban
   end
 
   def users
-    if current_user && (current_user.role == 'moderator' || current_user.role == 'admin')
+    if logged_in_as(['admin', 'moderator'])
       @users = User.order('uid DESC').limit(200)
     else
       flash[:error] = 'Only moderators can moderate other users.'
@@ -314,7 +314,7 @@ def users
   end
 
   def batch
-    if current_user && (current_user.role == 'moderator' || current_user.role == 'admin')
+    if logged_in_as(['admin', 'moderator'])
       nodes = 0
       users = []
       params[:ids].split(',').uniq.each do |nid|
@@ -333,26 +333,8 @@ def batch
     end
   end
 
-  def migrate
-    if current_user && current_user.role == 'admin'
-      du = User.find params[:id]
-      if du.user
-        flash[:error] = 'The user has already been migrated.'
-      else
-        if du.migrate
-          flash[:notice] = 'The user was migrated! Enthusiasm!'
-        else
-          flash[:error] = 'The user could not be migrated.'
-        end
-      end
-    else
-      flash[:error] = 'Only admins can migrate users.'
-    end
-    redirect_to '/profile/' + du.name
-  end
-
   def queue
-    if current_user && (current_user.role == 'moderator' || current_user.role == 'admin')
+    if logged_in_as(['admin', 'moderator'])
       @notes = Node.where(status: 4)
                    .paginate(page: params[:page])
       flash[:warning] = "These are notes requiring moderation. <a href='/wiki/moderation'>Community moderators</a> may approve or reject them."
  • 💾 Commit your changes

  • 🔀 Start a Pull Request. There are two ways how you can start a pull request:

  1. If you are familiar with the terminal or would like to learn it, here is a great tutorial on how to send a pull request using the terminal.

  2. You can also edit files directly in your browser and open a pull request from there.

  • 🏁 Done Ask in comments for a review :)

Please keep us updated

💬 - We encourage contributors to be respectful to the community and provide an update within a week of claiming a first-timers-only issue. We're happy to keep it assigned to you as long as you need if you update us with a request for more time or help, but if we don't see any activity a week after you claim it we may reassign it to give someone else a chance. Thank you in advance!

If this happens to you, don't sweat it! Grab another open issue.

Is someone else already working on this?

🔗- We encourage contributors to link to the original issue in their pull request so all users can easily see if someone's already started on it.

👥- If someone seems stuck, offer them some help! Otherwise, take a look at some other issues you can help with. Thanks!

🤔 Questions?

Leave a comment below!

@jywarren

This comment has been minimized.

Copy link
Contributor

commented Mar 29, 2019

Hi @jillpena I made this one for you! It is a bit long, but don't be concerned -- it's fairly repetitive! I also set it up so you remove some unwanted code.

jillpena added a commit to jillpena/plots2 that referenced this issue Apr 17, 2019

@jillpena

This comment has been minimized.

Copy link

commented Apr 17, 2019

🏁 Done and looking for a review :)

@gauravano

This comment has been minimized.

Copy link
Member

commented Apr 23, 2019

Reviewing..

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.