api_authorization 0.1.2 → 0.1.3

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 94acc2d7dd25207a51d40cfb954a679042f15a0b9d9b68c4fd36f75cfea3fbd9
4
- data.tar.gz: cdef5a4d61e6f6d83f8787016281ff959bd2e0e820fd45d9eb4c7030239dac98
3
+ metadata.gz: '058cb751c71438f05f1c62821556d1a9f5f3c12ab98a4d3dcebeaab49ffdb37a'
4
+ data.tar.gz: 159943ea62e15a42aee687d4f409b27ddfd0fd60396638b635e8830162aceab3
5
5
  SHA512:
6
- metadata.gz: ecf08b501bf862dcef4b1bb603c2255aee2c5db3d724a3aeafc171829d1cf303582cca2568aae82a85519e682a0809c8345880a2e6a62729663fea2ed06b8be4
7
- data.tar.gz: 46fd8d2142abd9038bbffd6c232a9a68df34c1beca042f3c0adfd07de2587f6b7675a71dc11957c1500621d9fcc289f9ebb4f2096ebac804f38c1b9f294ba368
6
+ metadata.gz: a2f0dd92a53740aae4997bd9443cc39f7353c71920da460c1b4f9a2334e329444ea86c84a077c814f6133431612b30bd56946ff9ff2c40081d10a746c4dd7e75
7
+ data.tar.gz: 812cf69c1b185414645fcf6be3989d49829ef7d0e92e65a911a55b837c2f29fbf8b5b4e3ce257ac7c13628f98c9bcaec4d73328923f3a7654844e14d384ac007
data/README.md CHANGED
@@ -27,11 +27,11 @@ $ gem install api_authorization
27
27
  ## Usage
28
28
  1. After you have created your users_table(through devise or manually) next run
29
29
  ```bash
30
- $ rails api_auth:initialize
30
+ $ rails api_auth:install
31
31
  ```
32
32
  2. Next populate permissions table with your controllers and actions run:
33
33
  ```bash
34
- $ rails api_auth:create_permissions
34
+ $ rails api_auth:re_populate_permissions
35
35
  ```
36
36
  3. Include the Authorization module on your `ApplicationController` :
37
37
  ```ruby
@@ -39,10 +39,20 @@ $ rails api_auth:create_permissions
39
39
  include ApiAuthorization
40
40
  enable_role_authorization
41
41
  ```
42
- 4. DONE
43
-
44
- ##### More CLI commands will be published soon
42
+ 4. You can create/read/update/delete roles and permissions for the user through [CLI commands](cli.MD) or by
43
+ using the dashboard which you need to install seperately by running:
44
+ ```bash
45
+ rails api_auth:install_dashboard
46
+ ```
47
+ 5. And after running the rails app visit:
48
+ ```
49
+ http://localhost:3000/auth_dashboard
50
+ ```
51
+ #
52
+ ### [CLI commands](cli.MD)
45
53
 
54
+ ### Managing Roles and Permissions
55
+ There are two ways to manage your roles and
46
56
  ## Contributing
47
57
  Feel free to suggest a feature or report a bug.
48
58
  #### [Code Of Conduct](CODE_OF_CONDUCT.md)
@@ -1,4 +1,4 @@
1
1
  #!/usr/bin/env ruby
2
2
  # frozen_string_literal: true
3
3
 
4
- rake api_auth: init
4
+ CLI.start(ARGV)
@@ -19,15 +19,19 @@ module ApiAuthorization
19
19
  roles = current_user.try(:roles)
20
20
  return if roles.find { |role| role.try(:name).try(:downcase) == 'superadmin' }
21
21
 
22
- return render json: { error: 'You are not authorized' }, status: 403 if current_user.roles.empty?
22
+ # return render json: { error: 'You are not authorized' }, status: 403 if current_user.roles.empty?
23
+ return not_authorized if current_user.roles.empty?
24
+
23
25
 
24
26
  current_user.roles.each do |role|
25
27
  return if role.permissions.where(controller: params['controller'], action: params['action']).count.positive?
26
28
  end
27
29
 
28
- render json: { error: 'You are not authorized' }, status: 403
30
+ # render json: { error: 'You are not authorized' }, status: 403
31
+ not_authorized
29
32
  rescue StandardError => e
30
- render json: { error: 'You are not authorized' }, status: 403
33
+ # render json: { error: 'You are not authorized' }, status: 403
34
+ not_authorized
31
35
  end
32
36
 
33
37
  # Filter the request params to know wether or not a user has
@@ -42,7 +46,6 @@ module ApiAuthorization
42
46
  # @params [Action::Parameters]
43
47
  # @return [Action::Parameters] without the disallowed key/values.
44
48
  def check_allowed_params(params, controller, action)
45
- puts 'AUTHORIZATION: Checking the request params' if Rails.env == 'development'
46
49
 
47
50
  roles = current_user.try(:permissions)
48
51
  return params if roles.find { |role| role.try(:name).try(:downcase) == 'superadmin' }
@@ -63,12 +66,29 @@ module ApiAuthorization
63
66
 
64
67
  params
65
68
  end
69
+
70
+ # a wrapper around respond_to helper that returns
71
+ # content as json, and html
72
+ #
73
+ # @params [String,Fixnum]
74
+ # @return [ActionController::MimeResponds]
75
+ def not_authorized(message = 'You are not authorized', status = 403)
76
+ respond_to do |format|
77
+ format.json do
78
+ return render json: { error: message }, status: status
79
+ end
80
+ format.html do
81
+ return render html: "<h2> Forbidden #{status}</h2>".html_safe, status: status
82
+ end
83
+ end
84
+ end
66
85
  end
67
86
 
68
87
  class Railtie < ::Rails::Railtie
69
- # exportin rake tasks
88
+ # exporting rake tasks
70
89
  rake_tasks do
71
- load 'tasks/initialize.rake'
90
+ load 'tasks/main_tasks.rake'
91
+ load 'tasks/dashboard_tasks.rake'
72
92
  load 'tasks/user_tasks.rake'
73
93
  end
74
94
  end
@@ -1,3 +1,3 @@
1
1
  module ApiAuthorization
2
- VERSION = '0.1.2'
2
+ VERSION = '0.1.3'
3
3
  end
@@ -0,0 +1,22 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ # frozen_string_literal: true
4
+
5
+ require 'thor'
6
+ class CLI < Thor
7
+ include Thor::Actions
8
+
9
+ def self.source_root
10
+ File.dirname(__FILE__)
11
+ end
12
+
13
+ desc 'append_has_and_belongs_to_many', 'append has_and_belongs_to_many'
14
+ def append_has_and_belongs_to_many(model_filename, belongs_to)
15
+ inject_into_file "./app/models/#{model_filename}.rb", " has_and_belongs_to_many :#{belongs_to}\n", before: /^end/
16
+ end
17
+
18
+ desc 'append_config', 'insert config code'
19
+ def append_config(config_file, config_code)
20
+ inject_into_file "./config/initializers/#{config_file}.rb", " #{config_code}\n", before: /^end/
21
+ end
22
+ end
@@ -0,0 +1,9 @@
1
+ # frozen_string_literal: true
2
+
3
+ namespace :api_auth do
4
+ desc 'Create the dashboard'
5
+ task :install_dashboard, :environment do |_t, args|
6
+ sh 'rails g rails_admin:install auth_dashboar', verbose: false
7
+ sh 'thor cli:append_config rails_admin "config.included_models = [\"User\", \"Role\", \"Permission\"]"', verbose: false
8
+ end
9
+ end
@@ -3,8 +3,8 @@
3
3
  require_relative './../app_routes/application_routes.rb'
4
4
 
5
5
  namespace :api_auth do
6
- desc 'Create the initial tables and populate the permissions table'
7
- task :init, %i[tenant single_role] => [:environment] do |_t, args|
6
+ desc 'Create the initial tables'
7
+ task :install, %i[tenant single_role] => [:environment] do |_t, args|
8
8
  Apartment::Tenant.switch! args[:tenant] if args[:tenant]
9
9
 
10
10
  puts 'Initializing'
@@ -14,25 +14,32 @@ namespace :api_auth do
14
14
 
15
15
  if args[:single_role].nil?
16
16
  # creating roles table
17
- sh 'rails g model role name:string description:text'
18
- Rake::Task['db:migrate'].invoke
17
+ sh 'rails g model role name:string description:text', verbose: false
18
+ sh 'rails db:migrate', verbose: false
19
+
20
+ # adding has_many_and_belongs_to with thor
21
+ sh 'thor cli:append_has_and_belongs_to_many role users', verbose: false
22
+ sh 'thor cli:append_has_and_belongs_to_many user roles', verbose: false
19
23
 
20
24
  # creating a join table between users and roles
21
- sh 'rails g migration CreateJoinTableUserRole user role'
22
- sh 'rails db:migrate'
25
+ sh 'rails g migration CreateJoinTableUserRole user role', verbose: false
26
+ sh 'rails db:migrate', verbose: false
23
27
 
24
28
  else
25
- sh 'rails g model role name:string description:text user:references'
26
- sh 'rails db:migrate'
29
+ sh 'rails g model role name:string description:text user:references', verbose: false
30
+ sh 'rails db:migrate', verbose: false
27
31
  end
28
32
 
29
33
  # creating permissions table
30
- sh 'rails g model permission controller:string action:string allowed_params:json'
31
- sh 'rails db:migrate'
34
+ sh 'rails g model permission controller:string action:string allowed_params:json', verbose: false
35
+ sh 'rails db:migrate', verbose: false
36
+
37
+ sh 'thor cli:append_has_and_belongs_to_many role permissions', verbose: false
38
+ sh 'thor cli:append_has_and_belongs_to_many permission roles', verbose: false
32
39
 
33
40
  # creating a join table between roles and permissions
34
- sh 'rails g migration CreateJoinTableRolePermission role permission'
35
- sh 'rails db:migrate'
41
+ sh 'rails g migration CreateJoinTableRolePermission role permission', verbose: false
42
+ sh 'rails db:migrate', verbose: false
36
43
 
37
44
  else
38
45
  puts 'users table does not exist !'
@@ -42,7 +49,7 @@ namespace :api_auth do
42
49
  end
43
50
 
44
51
  desc 'Populate the permissions table with all the controllers and actions of the application'
45
- task :create_permissions, %i[tenant] => [:environment] do |_t, args|
52
+ task :re_populate_permissions, %i[tenant] => [:environment] do |_t, args|
46
53
  Apartment::Tenant.switch! args[:tenant] if args[:tenant]
47
54
  AppRoutes::ApplicationRoutes.new
48
55
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: api_authorization
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.2
4
+ version: 0.1.3
5
5
  platform: ruby
6
6
  authors:
7
7
  - Giovanni Panasiti
@@ -9,7 +9,7 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2020-08-06 00:00:00.000000000 Z
12
+ date: 2020-08-12 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: rails
@@ -32,21 +32,7 @@ dependencies:
32
32
  - !ruby/object:Gem::Version
33
33
  version: 6.0.3.2
34
34
  - !ruby/object:Gem::Dependency
35
- name: sqlite3
36
- requirement: !ruby/object:Gem::Requirement
37
- requirements:
38
- - - ">="
39
- - !ruby/object:Gem::Version
40
- version: '0'
41
- type: :development
42
- prerelease: false
43
- version_requirements: !ruby/object:Gem::Requirement
44
- requirements:
45
- - - ">="
46
- - !ruby/object:Gem::Version
47
- version: '0'
48
- - !ruby/object:Gem::Dependency
49
- name: byebug
35
+ name: thor
50
36
  requirement: !ruby/object:Gem::Requirement
51
37
  requirements:
52
38
  - - ">="
@@ -77,7 +63,9 @@ files:
77
63
  - lib/api_authorization/railtie.rb
78
64
  - lib/api_authorization/version.rb
79
65
  - lib/app_routes/application_routes.rb
80
- - lib/tasks/initialize.rake
66
+ - lib/tasks/cli.thor
67
+ - lib/tasks/dashboard_tasks.rake
68
+ - lib/tasks/main_tasks.rake
81
69
  - lib/tasks/user_tasks.rake
82
70
  homepage: https://montedelgallo.com/
83
71
  licenses: