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

View file

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

View file

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

View file

@ -1,3 +1,5 @@
# frozen_string_literal: true
class AuthenticationController < ApplicationController
skip_before_action :authenticate_request
@ -5,7 +7,7 @@ class AuthenticationController < ApplicationController
command = AuthenticateUser.call(params[:email], params[:password])
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,
email: user.email }
else
@ -15,15 +17,15 @@ class AuthenticationController < ApplicationController
def signup
# Check if the installation allows user signups
if(ENV['DISABLE_SIGNUPS'] === "true")
render json: {}, status: 500
if (ENV["DISABLE_SIGNUPS"] === "true")
render json: {}, status: :internal_server_error
else
email = params[:email]
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)
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
end

View file

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

View file

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

View file

@ -1,22 +1,23 @@
# frozen_string_literal: true
class FolderAppsController < ApplicationController
def create
app_id = params[:app_id]
folder_id = params[:folder_id]
def create
app_id = params[:app_id]
folder_id = params[:folder_id]
@app = App.find app_id
@app = App.find app_id
unless AppPolicy.new(@current_user, @app).update?
render json: { message: 'Could not add app to folder due to insufficient permissions' }, status: 500
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
unless AppPolicy.new(@current_user, @app).update?
render json: { message: "Could not add app to folder due to insufficient permissions" }, status: :internal_server_error
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: :internal_server_error
end
end
end

View file

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

View file

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

View file

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

View file

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

View file

@ -1,3 +1,5 @@
# frozen_string_literal: true
class UsersController < ApplicationController
skip_before_action :authenticate_request
@ -6,13 +8,13 @@ class UsersController < ApplicationController
if user
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]
user.organization.update(name: params[:organization])
end
else
render json: { message: 'Invalid Invitation Token' }, status: :bad_request
render json: { message: "Invalid Invitation Token" }, status: :bad_request
end
end
end

View file

@ -1,12 +1,14 @@
# frozen_string_literal: true
class VersionsController < ApplicationController
def create
@app = App.find params[:app_id]
name = params[:version]['versionName']
name = params[:version]["versionName"]
AppVersion.create(app: @app, name: name)
end
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
def update