api_authorization 0.1.2 → 0.1.3
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
- data/README.md +15 -5
- data/bin/api_auth +1 -1
- data/lib/api_authorization/railtie.rb +26 -6
- data/lib/api_authorization/version.rb +1 -1
- data/lib/tasks/cli.thor +22 -0
- data/lib/tasks/dashboard_tasks.rake +9 -0
- data/lib/tasks/{initialize.rake → main_tasks.rake} +20 -13
- metadata +6 -18
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: '058cb751c71438f05f1c62821556d1a9f5f3c12ab98a4d3dcebeaab49ffdb37a'
|
4
|
+
data.tar.gz: 159943ea62e15a42aee687d4f409b27ddfd0fd60396638b635e8830162aceab3
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
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:
|
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:
|
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.
|
43
|
-
|
44
|
-
|
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)
|
data/bin/api_auth
CHANGED
@@ -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
|
-
#
|
88
|
+
# exporting rake tasks
|
70
89
|
rake_tasks do
|
71
|
-
load 'tasks/
|
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
|
data/lib/tasks/cli.thor
ADDED
@@ -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
|
7
|
-
task :
|
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
|
-
|
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 :
|
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.
|
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-
|
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:
|
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/
|
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:
|