Plots2: Refactor admin_controller with simpler user login/role check

Created on 29 Mar 2019  ยท  13Comments  ยท  Source: publiclab/plots2

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-only help wanted

Most helpful comment

๐Ÿ Done and looking for a review :)

All 13 comments

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.

๐Ÿ Done and looking for a review :)

Reviewing..

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

Hi @Saad2714, please go ahead. Thank you!

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 !!

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

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?

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 :)

Is this issue solved?
Please check it out!

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

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

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

Was this page helpful?
0 / 5 - 0 ratings

Related issues

jywarren picture jywarren  ยท  3Comments

keshavsethi picture keshavsethi  ยท  3Comments

milaaraujo picture milaaraujo  ยท  3Comments

first-timers[bot] picture first-timers[bot]  ยท  3Comments

RuthNjeri picture RuthNjeri  ยท  3Comments