From b1748c2981934c5d8c0a36c132be71d8cea1dc89 Mon Sep 17 00:00:00 2001 From: Alberto Gutierrez Date: Wed, 20 Dec 2017 20:42:58 +0100 Subject: [PATCH] Added admin route with set role and post user refresh, added fields followers and public repos --- app/controllers/application_controller.rb | 1 + app/controllers/v1/admin_controller.rb | 32 +++++++++++++++++++ app/jobs/refresh_users_job.rb | 26 +++++++++++++++ app/models/user.rb | 8 +++++ config/routes.rb | 4 +++ .../20171220174649_add_followers_to_user.rb | 11 +++++++ db/schema.rb | 6 ++-- 7 files changed, 86 insertions(+), 2 deletions(-) create mode 100644 app/controllers/v1/admin_controller.rb create mode 100644 app/jobs/refresh_users_job.rb create mode 100644 db/migrate/20171220174649_add_followers_to_user.rb diff --git a/app/controllers/application_controller.rb b/app/controllers/application_controller.rb index 4ac8823..3f1a602 100644 --- a/app/controllers/application_controller.rb +++ b/app/controllers/application_controller.rb @@ -1,2 +1,3 @@ class ApplicationController < ActionController::API + end diff --git a/app/controllers/v1/admin_controller.rb b/app/controllers/v1/admin_controller.rb new file mode 100644 index 0000000..36fa843 --- /dev/null +++ b/app/controllers/v1/admin_controller.rb @@ -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 diff --git a/app/jobs/refresh_users_job.rb b/app/jobs/refresh_users_job.rb new file mode 100644 index 0000000..39a55d3 --- /dev/null +++ b/app/jobs/refresh_users_job.rb @@ -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 diff --git a/app/models/user.rb b/app/models/user.rb index 5050863..49d8ba5 100644 --- a/app/models/user.rb +++ b/app/models/user.rb @@ -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| @@ -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 diff --git a/config/routes.rb b/config/routes.rb index e8e8a6a..168c8c9 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -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 diff --git a/db/migrate/20171220174649_add_followers_to_user.rb b/db/migrate/20171220174649_add_followers_to_user.rb new file mode 100644 index 0000000..5c87c25 --- /dev/null +++ b/db/migrate/20171220174649_add_followers_to_user.rb @@ -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 diff --git a/db/schema.rb b/db/schema.rb index a0cced3..27d739b 100644 --- a/db/schema.rb +++ b/db/schema.rb @@ -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" @@ -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" @@ -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" @@ -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