plots2 icon indicating copy to clipboard operation
plots2 copied to clipboard

Refactor admin_controller with simpler user login/role check

Open first-timers[bot] opened this issue 7 years ago • 13 comments

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!

first-timers[bot] avatar Mar 29 '19 21:03 first-timers[bot]

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.

jywarren avatar Mar 29 '19 21:03 jywarren

🏁 Done and looking for a review :)

jillpena avatar Apr 17 '19 02:04 jillpena

Reviewing..

grvsachdeva avatar Apr 23 '19 09:04 grvsachdeva

Hi, it seems that the issue is still open... Can I work on this issue ?? @gauravano @jywarren

Saad2714 avatar Jul 11 '19 02:07 Saad2714

Hi @Saad2714, please go ahead. Thank you!

grvsachdeva avatar Jul 17 '19 19:07 grvsachdeva

Hello sir, Actually, I have solved a "first-timers-only" issue recently. So I would like to leave this issue for someone new... What say @gauravano ?? Thanks !!

Saad2714 avatar Jul 24 '19 17:07 Saad2714

Sounds great. You can try other issues at https://github.com/publiclab/plots2/issues

grvsachdeva avatar Jul 28 '19 07:07 grvsachdeva

Hi! @gauravano @jywarren, I updated the spam_comments function in admin_controller with logged_in_as(['admin', 'moderator']). Is there anything else in the admin_controller need to be changed?

yuanxizhang avatar Aug 10 '19 02:08 yuanxizhang

I was wondering if this issue is still up for grabs? I'd be happy to help out here, since I'm looking for my first chance for an open source contribution :)

kolja-lucht avatar Oct 08 '19 07:10 kolja-lucht

Is this issue solved? Please check it out!

SidharthBansal avatar Nov 22 '19 19:11 SidharthBansal

Hello! May I be assigned to this issue? Thanks :)

guArroyo avatar Mar 16 '20 07:03 guArroyo

Can this issue be closed? When looking at the code it seems like this has been solved.

oedbro avatar Oct 03 '20 07:10 oedbro

Thanks for highlighting @oedbro. Yes, it's already solved. Closing the issue :)

grvsachdeva avatar Oct 03 '20 19:10 grvsachdeva