Rubocop fixes for controllers (#180)

* Improves ruby code in app/models/app_user.rb based on suggestions from Rubocop

* Rubocop fixes for more models

* Rubocop fixes for controllers
This commit is contained in:
Unnikrishnan KP 2021-06-01 12:39:07 +05:30 committed by GitHub
parent 47a816742c
commit ccadcb2e9d
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
13 changed files with 116 additions and 93 deletions

View file

@ -1,3 +1,5 @@
# frozen_string_literal: true
class AppUsersController < ApplicationController class AppUsersController < ApplicationController
def create def create
org_user_id = params[:org_user_id] org_user_id = params[:org_user_id]
@ -18,7 +20,7 @@ class AppUsersController < ApplicationController
if app_user.save if app_user.save
render json: { success: true } render json: { success: true }
else else
render json: { message: 'Could not create user' }, status: 500 render json: { message: "Could not create user" }, status: :internal_server_error
end end
end end
end end

View file

@ -1,3 +1,5 @@
# frozen_string_literal: true
class ApplicationController < ActionController::API class ApplicationController < ActionController::API
include Pundit include Pundit
rescue_from Pundit::NotAuthorizedError, with: :user_not_authorized rescue_from Pundit::NotAuthorizedError, with: :user_not_authorized
@ -7,12 +9,12 @@ class ApplicationController < ActionController::API
private private
def authenticate_request def authenticate_request
@current_user = AuthorizeApiRequest.call(request.headers).result @current_user = AuthorizeApiRequest.call(request.headers).result
render json: { error: 'Not Authorized' }, status: 401 unless @current_user render json: { error: "Not Authorized" }, status: :unauthorized unless @current_user
end end
def user_not_authorized def user_not_authorized
render json: { error: 'Access denied' }, status: :forbidden render json: { error: "Access denied" }, status: :forbidden
end end
end end

View file

@ -1,3 +1,5 @@
# frozen_string_literal: true
class AppsController < ApplicationController class AppsController < ApplicationController
skip_before_action :authenticate_request, only: [:show] skip_before_action :authenticate_request, only: [:show]
@ -13,44 +15,44 @@ class AppsController < ApplicationController
@scope = @folder.apps @scope = @folder.apps
end end
@apps = @scope.order('created_at desc') @apps = @scope.order("created_at desc")
.page(params[:page]) .page(params[:page])
.per(10) .per(10)
.includes(:user) .includes(:user)
@meta = { @meta = {
total_pages: @apps.total_pages, total_pages: @apps.total_pages,
folder_count: @scope.count, folder_count: @scope.count,
total_count: App.where(organization: @current_user.organization).count, total_count: App.where(organization: @current_user.organization).count,
current_page: @apps.current_page current_page: @apps.current_page
} }
end end
def create def create
authorize App authorize App
@app = App.create({ @app = App.create({
name: 'Untitled app', name: "Untitled app",
organization: @current_user.organization, organization: @current_user.organization,
current_version: AppVersion.new(name: 'v0'), current_version: AppVersion.new(name: "v0"),
user: @current_user user: @current_user
}) })
AppUser.create(app: @app, user: @current_user, role: 'admin') AppUser.create(app: @app, user: @current_user, role: "admin")
end end
def show def show
@app = App.find params[:id] @app = App.find params[:id]
# Logic to bypass auth for public apps # Logic to bypass auth for public apps
unless @app.is_public unless @app.is_public
authenticate_request authenticate_request
authorize @app authorize @app
end end
end end
def update def update
@app = App.find params[:id] @app = App.find params[:id]
authorize @app authorize @app
@app.update(params['app'].permit('name', 'current_version_id', 'is_public')) @app.update(params["app"].permit("name", "current_version_id", "is_public"))
end end
def users def users

View file

@ -1,3 +1,5 @@
# frozen_string_literal: true
class AuthenticationController < ApplicationController class AuthenticationController < ApplicationController
skip_before_action :authenticate_request skip_before_action :authenticate_request
@ -5,7 +7,7 @@ class AuthenticationController < ApplicationController
command = AuthenticateUser.call(params[:email], params[:password]) command = AuthenticateUser.call(params[:email], params[:password])
if command.success? if command.success?
user = User.find_by_email params[:email] user = User.find_by email: params[:email]
render json: { auth_token: command.result, first_name: user.first_name, last_name: user.last_name, render json: { auth_token: command.result, first_name: user.first_name, last_name: user.last_name,
email: user.email } email: user.email }
else else
@ -15,15 +17,15 @@ class AuthenticationController < ApplicationController
def signup def signup
# Check if the installation allows user signups # Check if the installation allows user signups
if(ENV['DISABLE_SIGNUPS'] === "true") if (ENV["DISABLE_SIGNUPS"] === "true")
render json: {}, status: 500 render json: {}, status: :internal_server_error
else else
email = params[:email] email = params[:email]
password = SecureRandom.uuid password = SecureRandom.uuid
org = Organization.create(name: 'new org') org = Organization.create(name: "new org")
user = User.create(email: email, password: password, organization: org, invitation_token: SecureRandom.uuid) user = User.create(email: email, password: password, organization: org, invitation_token: SecureRandom.uuid)
org_user = OrganizationUser.create(user: user, organization: org, role: 'admin') org_user = OrganizationUser.create(user: user, organization: org, role: "admin")
# UserMailer.with(user: user, sender: @current_user).new_signup_email.deliver if org_user.save # UserMailer.with(user: user, sender: @current_user).new_signup_email.deliver if org_user.save
end end

View file

@ -1,3 +1,5 @@
# frozen_string_literal: true
class DataQueriesController < ApplicationController class DataQueriesController < ApplicationController
skip_before_action :authenticate_request, only: [:run] skip_before_action :authenticate_request, only: [:run]
@ -15,11 +17,10 @@ class DataQueriesController < ApplicationController
) )
if @data_query.errors.present? if @data_query.errors.present?
render json: { message: 'Query could not be created' }, status: 500 render json: { message: "Query could not be created" }, status: :internal_server_error
else else
render json: { message: 'success' } render json: { message: "success" }
end end
end end
def update def update
@ -27,9 +28,9 @@ class DataQueriesController < ApplicationController
@data_query.update(options: params[:options], name: params[:name]) @data_query.update(options: params[:options], name: params[:name])
if @data_query.errors.present? if @data_query.errors.present?
render json: { message: 'Query could not be updated' }, status: 500 render json: { message: "Query could not be updated" }, status: :internal_server_error
else else
render json: { message: 'success' } render json: { message: "success" }
end end
end end

View file

@ -1,3 +1,5 @@
# frozen_string_literal: true
class DataSourcesController < ApplicationController class DataSourcesController < ApplicationController
def index def index
@data_sources = DataSource.where(app_id: params[:app_id]) @data_sources = DataSource.where(app_id: params[:app_id])
@ -8,17 +10,17 @@ class DataSourcesController < ApplicationController
options_to_save = {} options_to_save = {}
options.each do |option| options.each do |option|
if option['encrypted'] if option["encrypted"]
credential = Credential.create(value: option['value']) credential = Credential.create(value: option["value"])
options_to_save[option['key']] = { options_to_save[option["key"]] = {
credential_id: credential.id, credential_id: credential.id,
encrypted: option['encrypted'] encrypted: option["encrypted"]
} }
else else
options_to_save[option['key']] = { options_to_save[option["key"]] = {
value: option['value'], value: option["value"],
encrypted: option['encrypted'] encrypted: option["encrypted"]
} }
end end
end end
@ -38,17 +40,17 @@ class DataSourcesController < ApplicationController
options_to_save = {} options_to_save = {}
options.each do |option| options.each do |option|
if option['encrypted'] if option["encrypted"]
credential = Credential.create(value: option['value']) credential = Credential.create(value: option["value"])
options_to_save[option['key']] = { options_to_save[option["key"]] = {
credential_id: credential.id, credential_id: credential.id,
encrypted: option['encrypted'] encrypted: option["encrypted"]
} }
else else
options_to_save[option['key']] = { options_to_save[option["key"]] = {
value: option['value'], value: option["value"],
encrypted: option['encrypted'] encrypted: option["encrypted"]
} }
end end
end end
@ -67,27 +69,27 @@ class DataSourcesController < ApplicationController
render json: { status: 200 } render json: { status: 200 }
rescue StandardError => e rescue StandardError => e
puts e puts e
render json: { message: e }, status: 500 render json: { message: e }, status: :internal_server_error
end end
def authorize_oauth2 def authorize_oauth2
data_source = DataSource.find params[:data_source_id] data_source = DataSource.find params[:data_source_id]
options = CredentialService.new.decrypt_options(data_source.options) options = CredentialService.new.decrypt_options(data_source.options)
access_token_url = options['access_token_url'] access_token_url = options["access_token_url"]
custom_params = options['custom_auth_params'].to_h custom_params = options["custom_auth_params"].to_h
response = HTTParty.post(access_token_url, response = HTTParty.post(access_token_url,
body: { code: params[:code], body: { code: params[:code],
client_id: options['client_id'], client_id: options["client_id"],
client_secret: options['client_secret'], client_secret: options["client_secret"],
grant_type: options['grant_type'], grant_type: options["grant_type"],
redirect_uri: "#{ENV.fetch('TOOLJET_HOST')}/oauth2/authorize", redirect_uri: "#{ENV.fetch('TOOLJET_HOST')}/oauth2/authorize",
**custom_params }.to_json, **custom_params }.to_json,
headers: { 'Content-Type' => 'application/json' }) headers: { "Content-Type" => "application/json" })
result = JSON.parse(response.body) result = JSON.parse(response.body)
access_token = result['access_token'] access_token = result["access_token"]
options = { access_token: access_token } options = { access_token: access_token }
@ -108,20 +110,20 @@ class DataSourcesController < ApplicationController
render json: { url: url } render json: { url: url }
end end
private private
def fetch_oauth_options(options) def fetch_oauth_options(options)
# Fetch necessary access token if OAuth2 based data source # Fetch necessary access token if OAuth2 based data source
if options.find { |option| option['key'] == 'oauth2' } if options.find { |option| option["key"] == "oauth2" }
provider = options.find { |option| option['key'] === 'provider' } ['value'] provider = options.find { |option| option["key"] === "provider" } ["value"]
service_class = "#{provider.capitalize}OauthService".constantize service_class = "#{provider.capitalize}OauthService".constantize
access_info = service_class.fetch_access_token(options.find { |option| option['key'] === 'code' } ['value']) access_info = service_class.fetch_access_token(options.find { |option| option["key"] === "code" } ["value"])
options.reject! { |option| option['key'] == 'code' } options.reject! { |option| option["key"] == "code" }
access_info.each do |info| access_info.each do |info|
option = {} option = {}
option['key'] = info[0] option["key"] = info[0]
option['value'] = info[1] option["value"] = info[1]
option['encrypted'] = true option["encrypted"] = true
options << option options << option
end end
end end

View file

@ -1,22 +1,23 @@
# frozen_string_literal: true
class FolderAppsController < ApplicationController class FolderAppsController < ApplicationController
def create
app_id = params[:app_id]
folder_id = params[:folder_id]
def create @app = App.find app_id
app_id = params[:app_id]
folder_id = params[:folder_id]
@app = App.find app_id
unless AppPolicy.new(@current_user, @app).update? unless AppPolicy.new(@current_user, @app).update?
render json: { message: 'Could not add app to folder due to insufficient permissions' }, status: 500 render json: { message: "Could not add app to folder due to insufficient permissions" }, status: :internal_server_error
return return
end
folder_app = FolderApp.new(app_id: app_id, folder_id: folder_id)
if folder_app.save
render json: {}
else
render json: { message: 'App already in folder' }, status: 500
end
end end
folder_app = FolderApp.new(app_id: app_id, folder_id: folder_id)
if folder_app.save
render json: {}
else
render json: { message: "App already in folder" }, status: :internal_server_error
end
end
end end

View file

@ -1,10 +1,11 @@
# frozen_string_literal: true
class FoldersController < ApplicationController class FoldersController < ApplicationController
def index
@folders = Folder.where(organization: @current_user.organization)
end
def index def create
@folders = Folder.where(organization: @current_user.organization) Folder.create(name: params[:name], organization: @current_user.organization)
end end
def create
Folder.create(name: params[:name], organization: @current_user.organization)
end
end end

View file

@ -1,3 +1,5 @@
# frozen_string_literal: true
class OrganizationUsersController < ApplicationController class OrganizationUsersController < ApplicationController
def create def create
authorize OrganizationUser authorize OrganizationUser

View file

@ -1,3 +1,5 @@
# frozen_string_literal: true
class OrganizationsController < ApplicationController class OrganizationsController < ApplicationController
def users def users
@org_users = OrganizationUser.where(organization: @current_user.organization).includes(:user) @org_users = OrganizationUser.where(organization: @current_user.organization).includes(:user)

View file

@ -1,7 +1,9 @@
# frozen_string_literal: true
class ProbeController < ApplicationController class ProbeController < ApplicationController
skip_before_action :authenticate_request skip_before_action :authenticate_request
def health_check def health_check
render json: { works: 'yeah' } render json: { works: "yeah" }
end end
end end

View file

@ -1,3 +1,5 @@
# frozen_string_literal: true
class UsersController < ApplicationController class UsersController < ApplicationController
skip_before_action :authenticate_request skip_before_action :authenticate_request
@ -6,13 +8,13 @@ class UsersController < ApplicationController
if user if user
user.update(first_name: params[:first_name], last_name: params[:last_name], password: params[:password], invitation_token: nil) user.update(first_name: params[:first_name], last_name: params[:last_name], password: params[:password], invitation_token: nil)
user.organization_users.first.update(status: 'active') user.organization_users.first.update(status: "active")
if params[:new_signup] if params[:new_signup]
user.organization.update(name: params[:organization]) user.organization.update(name: params[:organization])
end end
else else
render json: { message: 'Invalid Invitation Token' }, status: :bad_request render json: { message: "Invalid Invitation Token" }, status: :bad_request
end end
end end
end end

View file

@ -1,12 +1,14 @@
# frozen_string_literal: true
class VersionsController < ApplicationController class VersionsController < ApplicationController
def create def create
@app = App.find params[:app_id] @app = App.find params[:app_id]
name = params[:version]['versionName'] name = params[:version]["versionName"]
AppVersion.create(app: @app, name: name) AppVersion.create(app: @app, name: name)
end end
def index def index
@versions = AppVersion.where(app_id: params['app_id']).order('created_at desc') @versions = AppVersion.where(app_id: params["app_id"]).order("created_at desc")
end end
def update def update