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

[WIP] Added admin route with set role and post user refresh, added fields f… #44

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions app/controllers/application_controller.rb
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
class ApplicationController < ActionController::API

end
32 changes: 32 additions & 0 deletions app/controllers/v1/admin_controller.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
module V1
##
# Admin controller
# Provides admin operations
##
class AdminController < ApplicationController
before_action :authenticate_user!
before_action :authenticate_admin!

def authenticate_admin!
render json: {error: "You need be an administrator"}, status: :method_not_allowed unless current_user.admin?
end

# Refresh all users
def users_refresh
job = RefreshUsersJob.perform_later()
render json: { data: job.job_id, metadata: { queue: job.queue_name, priority: job.priority } }, status: :accepted
end

# Set admin or staff or user
ROLES_ACCEPTED= %w[staff admin user]
def user_set_role
@user = User.find_by(id: params[:id]) || User.find_by(github_login: params[:id])
if ROLES_ACCEPTED.include? params[:role].downcase
@user.set_role(params[:role])
render status: :accepted
else
render json: {error: "You need to set a staff,admin or user role"}, status: :not_acceptable
end
end
end
end
26 changes: 26 additions & 0 deletions app/jobs/refresh_users_job.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
###
# __Background Job to refresh user spins__
# Will refresh all repos for a user and analyze them
# It will update them if found adfads
#
# @param user: User
# @return boolean
#
class RefreshUsersJob < ApplicationJob
include SourceControlHelper

queue_as :default

def perform()
logger.info "Refresh Users"
# Get the client using the application id (only public information)
# Find the spins in the database, store them as an array
User.all.each do |user|
data = Octokit.user user.github_login
user.update(
followers:data.followers,
public_repos: data.public_repos
)
end
end
end
8 changes: 8 additions & 0 deletions app/models/user.rb
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,12 @@ class User < ApplicationRecord
validates :email, presence: true
validates :sign_in_count, numericality: true

def set_role(param)
hash = {admin: false, staff: false}
hash[param.to_sym] = true unless param == 'user'
update(hash)
end

def self.return_user(github_user)
return nil if github_user&.id.nil?
User.where(id: github_user.id).first_or_create do |user|
Expand All @@ -66,6 +72,8 @@ def self.return_user(github_user)
user.github_type = github_user.type
user.github_blog = github_user.blog || ''
user.github_location = github_user.location || ''
user.followers = github_user.followers
user.public_repos = github_user.public_repos
user.email = github_user.email
user.github_bio = github_user.bio || ''
user.github_created_at = github_user.created_at
Expand Down
4 changes: 4 additions & 0 deletions config/routes.rb
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,10 @@
end
end
end


post '/admin/users/refresh', to: 'admin#users_refresh'
post '/admin/users/:id/:role', to: 'admin#user_set_role'
end


Expand Down
11 changes: 11 additions & 0 deletions db/migrate/20171220174649_add_followers_to_user.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
class AddFollowersToUser < ActiveRecord::Migration[5.1]
def up
add_column :users, :followers, :integer, default: 0
add_column :users, :public_repos, :integer, default: 0
end

def down
remove_column :users, :followers
remove_column :users, :public_repos
end
end
6 changes: 4 additions & 2 deletions db/schema.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
#
# It's strongly recommended that you check this file into your version control system.

ActiveRecord::Schema.define(version: 20171219101016) do
ActiveRecord::Schema.define(version: 20171220174649) do

# These are extensions that must be enabled in order to support this database
enable_extension "plpgsql"
Expand Down Expand Up @@ -46,6 +46,7 @@
t.datetime "gh_updated_at"
t.boolean "gh_archived", default: false
t.string "default_branch", default: "master"
t.text "readme"
t.string "license_key"
t.string "license_name"
t.string "license_html_url"
Expand All @@ -59,7 +60,6 @@
t.text "company"
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
t.text "readme", default: ""
t.string "user_login"
t.index ["published"], name: "index_spins_on_published"
t.index ["user_id"], name: "index_spins_on_user_id"
Expand Down Expand Up @@ -106,6 +106,8 @@
t.inet "last_sign_in_ip"
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
t.integer "followers", default: 0
t.integer "public_repos", default: 0
t.index ["github_id"], name: "index_users_on_github_id", unique: true
end

Expand Down