devise_can 0.1.1 → 1.0.0.pre.beta
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/devise_can.gemspec +2 -1
- data/lib/devise_can/version.rb +1 -1
- data/lib/generators/association/association_generator.rb +87 -0
- data/lib/generators/association/templates/role_migration.rb +14 -0
- data/lib/generators/association/templates/role_migration_existing.rb +11 -0
- data/lib/generators/association/templates/role_model.rb +6 -0
- data/lib/generators/association/templates/role_model_existing.rb +3 -0
- data/lib/generators/association/templates/user_role_migration.rb +14 -0
- data/lib/generators/association/templates/user_role_migration_existing.rb +12 -0
- data/lib/generators/association/templates/user_role_model.rb +7 -0
- data/lib/generators/association/templates/user_role_model_existing.rb +5 -0
- data/lib/generators/permission/permission_generator.rb +102 -0
- data/lib/generators/permission/templates/ability_model.rb +46 -0
- data/lib/generators/permission/templates/module_action_migration.rb +14 -0
- data/lib/generators/permission/templates/module_action_migration_existing.rb +11 -0
- data/lib/generators/permission/templates/module_action_model.rb +6 -0
- data/lib/generators/permission/templates/module_group_migration.rb +14 -0
- data/lib/generators/permission/templates/module_group_migration_existing.rb +11 -0
- data/lib/generators/permission/templates/module_group_model.rb +6 -0
- data/lib/generators/permission/templates/module_permission_migration.rb +14 -0
- data/lib/generators/permission/templates/module_permission_migration_existing.rb +13 -0
- data/lib/generators/permission/templates/module_permission_model.rb +7 -0
- metadata +38 -4
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 5915c881a06176374a7e1583507eefeab4df7d32
|
4
|
+
data.tar.gz: 01827a46927d990174d08a1a19c3b6644cf08bf3
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: cff298fcd9b0b3d32cf808560af7006a28a66155ca98254eddac26e69dbd365bccb5a8785ad11910c7fabb4bd0e34ca47961dd70d2cdc757999ee590a4acd51a
|
7
|
+
data.tar.gz: 5647a07ac614cbc1b43671cd9881f88d917d2c245899363d416a5ab5a8ee2c3a831d79b13b127e1f5432c1dd6480045430845dcb95d175e0845000a43ff4dddd
|
data/devise_can.gemspec
CHANGED
@@ -10,7 +10,7 @@ Gem::Specification.new do |spec|
|
|
10
10
|
spec.email = ["Raghav.Singh@lptpl.com"]
|
11
11
|
|
12
12
|
spec.summary = %q{This gem is to use for devise,cancancan and user role association.}
|
13
|
-
spec.description = %q{Enjoy the
|
13
|
+
spec.description = %q{Enjoy the functionality of this gem.}
|
14
14
|
spec.homepage = "https://github.com/RaghavVishnoi/devise_can.git"
|
15
15
|
spec.license = "MIT"
|
16
16
|
|
@@ -33,5 +33,6 @@ Gem::Specification.new do |spec|
|
|
33
33
|
spec.add_development_dependency "bundler", "~> 1.13"
|
34
34
|
spec.add_development_dependency "rake", "~> 10.0"
|
35
35
|
spec.add_dependency "devise"
|
36
|
+
spec.add_dependency "cancancan"
|
36
37
|
|
37
38
|
end
|
data/lib/devise_can/version.rb
CHANGED
@@ -0,0 +1,87 @@
|
|
1
|
+
require 'rails/generators/active_record'
|
2
|
+
class AssociationGenerator < ActiveRecord::Generators::Base
|
3
|
+
source_root File.expand_path('../templates', __FILE__)
|
4
|
+
|
5
|
+
argument :attributes, type: :array, default: [], banner: "field:type field:type"
|
6
|
+
|
7
|
+
def copy_role_migration
|
8
|
+
if (behavior == :invoke && model_exists?("role")) || (behavior == :revoke && role_migration_exists?)
|
9
|
+
migration_template "role_migration_existing.rb", "db/migrate/add_name_to_roles.rb", migration_version: migration_version
|
10
|
+
else
|
11
|
+
migration_template "role_migration.rb", "db/migrate/create_roles.rb", migration_version: migration_version
|
12
|
+
end
|
13
|
+
end
|
14
|
+
|
15
|
+
def copy_user_roles_migration
|
16
|
+
if (behavior == :invoke && model_exists?("user_role")) || (behavior == :revoke && user_role_migration_exists?)
|
17
|
+
migration_template "user_role_migration_existing.rb", "db/migrate/add_user_roles_association_to_user_roles.rb", migration_version: migration_version
|
18
|
+
else
|
19
|
+
migration_template "user_role_migration.rb", "db/migrate/create_user_roles.rb", migration_version: migration_version
|
20
|
+
end
|
21
|
+
end
|
22
|
+
|
23
|
+
def copy_role_model
|
24
|
+
if model_exists?("role")
|
25
|
+
copy_file "role_model_existing.rb","app/models/role.rb"
|
26
|
+
else
|
27
|
+
copy_file "role_model.rb","app/models/role.rb"
|
28
|
+
end
|
29
|
+
end
|
30
|
+
|
31
|
+
def copy_user_role_model
|
32
|
+
if model_exists?("user_role")
|
33
|
+
copy_file "user_role_model_existing.rb","app/models/user_role.rb"
|
34
|
+
else
|
35
|
+
copy_file "user_role_model.rb","app/models/user_role.rb"
|
36
|
+
end
|
37
|
+
end
|
38
|
+
|
39
|
+
|
40
|
+
private
|
41
|
+
def model_exists?(model_name)
|
42
|
+
File.exist?(File.join(destination_root, model_path(model_name)))
|
43
|
+
end
|
44
|
+
|
45
|
+
def role_migration_exists?
|
46
|
+
Dir.glob("#{File.join(destination_root, migration_path)}/[0-9]*_*.rb").grep(/\d+_add_name_to_roles.rb$/).first
|
47
|
+
end
|
48
|
+
|
49
|
+
def user_role_migration_exists?
|
50
|
+
Dir.glob("#{File.join(destination_root, migration_path)}/[0-9]*_*.rb").grep(/\d+_add_user_roles_association_to_user_roles.rb$/).first
|
51
|
+
end
|
52
|
+
|
53
|
+
def migration_path
|
54
|
+
@migration_path ||= File.join("db", "migrate")
|
55
|
+
end
|
56
|
+
|
57
|
+
def model_path(model_name)
|
58
|
+
File.join("app", "models", "#{model_name}.rb")
|
59
|
+
end
|
60
|
+
|
61
|
+
def migration_version
|
62
|
+
if rails5?
|
63
|
+
"[#{Rails::VERSION::MAJOR}.#{Rails::VERSION::MINOR}]"
|
64
|
+
end
|
65
|
+
end
|
66
|
+
|
67
|
+
def rails5?
|
68
|
+
Rails.version.start_with? '5'
|
69
|
+
end
|
70
|
+
|
71
|
+
def role_migration_data
|
72
|
+
<<RUBY
|
73
|
+
t.string :name
|
74
|
+
|
75
|
+
RUBY
|
76
|
+
end
|
77
|
+
|
78
|
+
def user_role_migration_data
|
79
|
+
<<RUBY
|
80
|
+
t.belongs_to :user
|
81
|
+
t.belongs_to :role
|
82
|
+
RUBY
|
83
|
+
end
|
84
|
+
|
85
|
+
|
86
|
+
|
87
|
+
end
|
@@ -0,0 +1,14 @@
|
|
1
|
+
class CreateRoles < ActiveRecord::Migration
|
2
|
+
def change
|
3
|
+
create_table :roles do |t|
|
4
|
+
<%= role_migration_data -%>
|
5
|
+
|
6
|
+
<% attributes.each do |attribute| -%>
|
7
|
+
t.<%= attribute.type %> :<%= attribute.name %>
|
8
|
+
<% end -%>
|
9
|
+
|
10
|
+
t.timestamps null: false
|
11
|
+
end
|
12
|
+
|
13
|
+
end
|
14
|
+
end
|
@@ -0,0 +1,11 @@
|
|
1
|
+
class AddNameToRoles < ActiveRecord::Migration
|
2
|
+
def self.up
|
3
|
+
change_column :roles,:name,:string
|
4
|
+
end
|
5
|
+
|
6
|
+
def self.down
|
7
|
+
# By default, we don't want to make any assumption about how to roll back a migration when your
|
8
|
+
# model already existed. Please edit below which fields you would like to remove in this migration.
|
9
|
+
raise ActiveRecord::IrreversibleMigration
|
10
|
+
end
|
11
|
+
end
|
@@ -0,0 +1,14 @@
|
|
1
|
+
class CreateUserRoles < ActiveRecord::Migration
|
2
|
+
def change
|
3
|
+
create_table :user_roles do |t|
|
4
|
+
<%= user_role_migration_data -%>
|
5
|
+
|
6
|
+
<% attributes.each do |attribute| -%>
|
7
|
+
t.<%= attribute.type %> :<%= attribute.name %>
|
8
|
+
<% end -%>
|
9
|
+
|
10
|
+
t.timestamps null: false
|
11
|
+
end
|
12
|
+
|
13
|
+
end
|
14
|
+
end
|
@@ -0,0 +1,12 @@
|
|
1
|
+
class AddUserRolesAssociationToUserRoles < ActiveRecord::Migration
|
2
|
+
def self.up
|
3
|
+
change_column :user_roles,:user_id,:integer,index: true
|
4
|
+
change_column :user_roles,:role_id,:integer,index: true
|
5
|
+
end
|
6
|
+
|
7
|
+
def self.down
|
8
|
+
# By default, we don't want to make any assumption about how to roll back a migration when your
|
9
|
+
# model already existed. Please edit below which fields you would like to remove in this migration.
|
10
|
+
raise ActiveRecord::IrreversibleMigration
|
11
|
+
end
|
12
|
+
end
|
@@ -0,0 +1,102 @@
|
|
1
|
+
require 'rails/generators/active_record'
|
2
|
+
class PermissionGenerator < ActiveRecord::Generators::Base
|
3
|
+
source_root File.expand_path('../templates', __FILE__)
|
4
|
+
|
5
|
+
argument :attributes, type: :array, default: [], banner: "field:type field:type"
|
6
|
+
|
7
|
+
|
8
|
+
def copy_module_group_migration
|
9
|
+
if (behavior == :invoke && model_exists?("module_group")) || (behavior == :revoke && role_migration_exists?)
|
10
|
+
migration_template "module_group_migration_existing.rb", "db/migrate/add_name_to_module_group.rb", migration_version: migration_version
|
11
|
+
else
|
12
|
+
migration_template "module_group_migration.rb", "db/migrate/create_module_group.rb", migration_version: migration_version
|
13
|
+
end
|
14
|
+
end
|
15
|
+
|
16
|
+
def copy_module_action_migration
|
17
|
+
if (behavior == :invoke && model_exists?("module_action")) || (behavior == :revoke && role_migration_exists?)
|
18
|
+
migration_template "module_action_migration_existing.rb", "db/migrate/add_name_to_module_action.rb", migration_version: migration_version
|
19
|
+
else
|
20
|
+
migration_template "module_action_migration.rb", "db/migrate/create_module_action.rb", migration_version: migration_version
|
21
|
+
end
|
22
|
+
end
|
23
|
+
|
24
|
+
def copy_module_permission_migration
|
25
|
+
if (behavior == :invoke && model_exists?("module_permission")) || (behavior == :revoke && role_migration_exists?)
|
26
|
+
migration_template "module_permission_migration_existing.rb", "db/migrate/add_group_action_to_module_permission.rb", migration_version: migration_version
|
27
|
+
else
|
28
|
+
migration_template "module_permission_migration.rb", "db/migrate/create_module_permission.rb", migration_version: migration_version
|
29
|
+
end
|
30
|
+
end
|
31
|
+
|
32
|
+
def copy_module_action_model
|
33
|
+
copy_file "module_action_model.rb","app/models/module_action.rb"
|
34
|
+
end
|
35
|
+
|
36
|
+
def copy_module_group_model
|
37
|
+
copy_file "module_group_model.rb","app/models/module_group.rb"
|
38
|
+
end
|
39
|
+
|
40
|
+
def copy_module_permission_model
|
41
|
+
copy_file "module_permission_model.rb","app/models/module_permission.rb"
|
42
|
+
end
|
43
|
+
|
44
|
+
def copy_ability
|
45
|
+
copy_file "ability_model.rb","app/models/ability.rb"
|
46
|
+
end
|
47
|
+
|
48
|
+
private
|
49
|
+
def model_exists?(model_name)
|
50
|
+
File.exist?(File.join(destination_root, model_path(model_name)))
|
51
|
+
end
|
52
|
+
|
53
|
+
def role_migration_exists?
|
54
|
+
Dir.glob("#{File.join(destination_root, migration_path)}/[0-9]*_*.rb").grep(/\d+_add_name_to_roles.rb$/).first
|
55
|
+
end
|
56
|
+
|
57
|
+
def user_role_migration_exists?
|
58
|
+
Dir.glob("#{File.join(destination_root, migration_path)}/[0-9]*_*.rb").grep(/\d+_add_user_roles_association_to_user_roles.rb$/).first
|
59
|
+
end
|
60
|
+
|
61
|
+
def migration_path
|
62
|
+
@migration_path ||= File.join("db", "migrate")
|
63
|
+
end
|
64
|
+
|
65
|
+
def model_path(model_name)
|
66
|
+
File.join("app", "models", "#{model_name}.rb")
|
67
|
+
end
|
68
|
+
|
69
|
+
def migration_version
|
70
|
+
if rails5?
|
71
|
+
"[#{Rails::VERSION::MAJOR}.#{Rails::VERSION::MINOR}]"
|
72
|
+
end
|
73
|
+
end
|
74
|
+
|
75
|
+
def rails5?
|
76
|
+
Rails.version.start_with? '5'
|
77
|
+
end
|
78
|
+
|
79
|
+
def module_group_migration_data
|
80
|
+
<<RUBY
|
81
|
+
t.string :name
|
82
|
+
|
83
|
+
RUBY
|
84
|
+
end
|
85
|
+
|
86
|
+
def module_action_migration_data
|
87
|
+
<<RUBY
|
88
|
+
t.string :name
|
89
|
+
|
90
|
+
RUBY
|
91
|
+
end
|
92
|
+
|
93
|
+
def module_permission_migration_data
|
94
|
+
<<RUBY
|
95
|
+
t.belongs_to :module_group
|
96
|
+
t.belongs_to :module_action
|
97
|
+
t.belongs_to :role
|
98
|
+
RUBY
|
99
|
+
end
|
100
|
+
|
101
|
+
|
102
|
+
end
|
@@ -0,0 +1,46 @@
|
|
1
|
+
class Ability
|
2
|
+
include CanCan::Ability
|
3
|
+
|
4
|
+
def initialize(user)
|
5
|
+
# Define abilities for the passed in user here. For example:
|
6
|
+
#
|
7
|
+
# user ||= User.new # guest user (not logged in)
|
8
|
+
# if user.admin?
|
9
|
+
# can :manage, :all
|
10
|
+
# else
|
11
|
+
# can :read, :all
|
12
|
+
# end
|
13
|
+
#
|
14
|
+
# The first argument to `can` is the action you are giving the user
|
15
|
+
# permission to do.
|
16
|
+
# If you pass :manage it will apply to every action. Other common actions
|
17
|
+
# here are :read, :create, :update and :destroy.
|
18
|
+
#
|
19
|
+
# The second argument is the resource the user can perform the action on.
|
20
|
+
# If you pass :all it will apply to every resource. Otherwise pass a Ruby
|
21
|
+
# class of the resource.
|
22
|
+
#
|
23
|
+
# The third argument is an optional hash of conditions to further filter the
|
24
|
+
# objects.
|
25
|
+
# For example, here the user can only update published articles.
|
26
|
+
#
|
27
|
+
# can :update, Article, :published => true
|
28
|
+
#
|
29
|
+
# See the wiki for details:
|
30
|
+
# https://github.com/CanCanCommunity/cancancan/wiki/Defining-Abilities
|
31
|
+
|
32
|
+
if user.roles.pluck(:name).include?('superadmin')
|
33
|
+
can :manage, :all
|
34
|
+
else
|
35
|
+
user.roles.map{|role| role.module_permissions}.each do |permissions|
|
36
|
+
permissions.each do |permission|
|
37
|
+
can permission.module_action.name.to_sym,permission.module_group.name.constantize
|
38
|
+
end
|
39
|
+
end
|
40
|
+
end
|
41
|
+
|
42
|
+
|
43
|
+
|
44
|
+
|
45
|
+
end
|
46
|
+
end
|
@@ -0,0 +1,14 @@
|
|
1
|
+
class CreateModuleAction < ActiveRecord::Migration
|
2
|
+
def change
|
3
|
+
create_table :module_actions do |t|
|
4
|
+
<%= module_action_migration_data -%>
|
5
|
+
|
6
|
+
<% attributes.each do |attribute| -%>
|
7
|
+
t.<%= attribute.type %> :<%= attribute.name %>
|
8
|
+
<% end -%>
|
9
|
+
|
10
|
+
t.timestamps null: false
|
11
|
+
end
|
12
|
+
|
13
|
+
end
|
14
|
+
end
|
@@ -0,0 +1,11 @@
|
|
1
|
+
class AddNameToModuleAction < ActiveRecord::Migration
|
2
|
+
def self.up
|
3
|
+
change_column :module_actions,:name,:string
|
4
|
+
end
|
5
|
+
|
6
|
+
def self.down
|
7
|
+
# By default, we don't want to make any assumption about how to roll back a migration when your
|
8
|
+
# model already existed. Please edit below which fields you would like to remove in this migration.
|
9
|
+
raise ActiveRecord::IrreversibleMigration
|
10
|
+
end
|
11
|
+
end
|
@@ -0,0 +1,14 @@
|
|
1
|
+
class CreateModuleGroup < ActiveRecord::Migration
|
2
|
+
def change
|
3
|
+
create_table :module_groups do |t|
|
4
|
+
<%= module_group_migration_data -%>
|
5
|
+
|
6
|
+
<% attributes.each do |attribute| -%>
|
7
|
+
t.<%= attribute.type %> :<%= attribute.name %>
|
8
|
+
<% end -%>
|
9
|
+
|
10
|
+
t.timestamps null: false
|
11
|
+
end
|
12
|
+
|
13
|
+
end
|
14
|
+
end
|
@@ -0,0 +1,11 @@
|
|
1
|
+
class AddNameToModuleGroup < ActiveRecord::Migration
|
2
|
+
def self.up
|
3
|
+
change_column :module_groups,:name,:string
|
4
|
+
end
|
5
|
+
|
6
|
+
def self.down
|
7
|
+
# By default, we don't want to make any assumption about how to roll back a migration when your
|
8
|
+
# model already existed. Please edit below which fields you would like to remove in this migration.
|
9
|
+
raise ActiveRecord::IrreversibleMigration
|
10
|
+
end
|
11
|
+
end
|
@@ -0,0 +1,14 @@
|
|
1
|
+
class CreateModulePermission < ActiveRecord::Migration
|
2
|
+
def change
|
3
|
+
create_table :module_permissions do |t|
|
4
|
+
<%= module_permission_migration_data -%>
|
5
|
+
|
6
|
+
<% attributes.each do |attribute| -%>
|
7
|
+
t.<%= attribute.type %> :<%= attribute.name %>
|
8
|
+
<% end -%>
|
9
|
+
|
10
|
+
t.timestamps null: false
|
11
|
+
end
|
12
|
+
|
13
|
+
end
|
14
|
+
end
|
@@ -0,0 +1,13 @@
|
|
1
|
+
class AddGroupActionToModulePermission < ActiveRecord::Migration
|
2
|
+
def self.up
|
3
|
+
change_column :module_permissions,:module_group_id,:integer,index: true
|
4
|
+
change_column :module_permissions,:module_action_id,:integer,index: true
|
5
|
+
change_column :module_permissions,:role_id,:integer,index: true
|
6
|
+
end
|
7
|
+
|
8
|
+
def self.down
|
9
|
+
# By default, we don't want to make any assumption about how to roll back a migration when your
|
10
|
+
# model already existed. Please edit below which fields you would like to remove in this migration.
|
11
|
+
raise ActiveRecord::IrreversibleMigration
|
12
|
+
end
|
13
|
+
end
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: devise_can
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.
|
4
|
+
version: 1.0.0.pre.beta
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- RaghavSingh
|
@@ -52,7 +52,21 @@ dependencies:
|
|
52
52
|
- - ">="
|
53
53
|
- !ruby/object:Gem::Version
|
54
54
|
version: '0'
|
55
|
-
|
55
|
+
- !ruby/object:Gem::Dependency
|
56
|
+
name: cancancan
|
57
|
+
requirement: !ruby/object:Gem::Requirement
|
58
|
+
requirements:
|
59
|
+
- - ">="
|
60
|
+
- !ruby/object:Gem::Version
|
61
|
+
version: '0'
|
62
|
+
type: :runtime
|
63
|
+
prerelease: false
|
64
|
+
version_requirements: !ruby/object:Gem::Requirement
|
65
|
+
requirements:
|
66
|
+
- - ">="
|
67
|
+
- !ruby/object:Gem::Version
|
68
|
+
version: '0'
|
69
|
+
description: Enjoy the functionality of this gem.
|
56
70
|
email:
|
57
71
|
- Raghav.Singh@lptpl.com
|
58
72
|
executables: []
|
@@ -70,6 +84,26 @@ files:
|
|
70
84
|
- devise_can.gemspec
|
71
85
|
- lib/devise_can.rb
|
72
86
|
- lib/devise_can/version.rb
|
87
|
+
- lib/generators/association/association_generator.rb
|
88
|
+
- lib/generators/association/templates/role_migration.rb
|
89
|
+
- lib/generators/association/templates/role_migration_existing.rb
|
90
|
+
- lib/generators/association/templates/role_model.rb
|
91
|
+
- lib/generators/association/templates/role_model_existing.rb
|
92
|
+
- lib/generators/association/templates/user_role_migration.rb
|
93
|
+
- lib/generators/association/templates/user_role_migration_existing.rb
|
94
|
+
- lib/generators/association/templates/user_role_model.rb
|
95
|
+
- lib/generators/association/templates/user_role_model_existing.rb
|
96
|
+
- lib/generators/permission/permission_generator.rb
|
97
|
+
- lib/generators/permission/templates/ability_model.rb
|
98
|
+
- lib/generators/permission/templates/module_action_migration.rb
|
99
|
+
- lib/generators/permission/templates/module_action_migration_existing.rb
|
100
|
+
- lib/generators/permission/templates/module_action_model.rb
|
101
|
+
- lib/generators/permission/templates/module_group_migration.rb
|
102
|
+
- lib/generators/permission/templates/module_group_migration_existing.rb
|
103
|
+
- lib/generators/permission/templates/module_group_model.rb
|
104
|
+
- lib/generators/permission/templates/module_permission_migration.rb
|
105
|
+
- lib/generators/permission/templates/module_permission_migration_existing.rb
|
106
|
+
- lib/generators/permission/templates/module_permission_model.rb
|
73
107
|
homepage: https://github.com/RaghavVishnoi/devise_can.git
|
74
108
|
licenses:
|
75
109
|
- MIT
|
@@ -85,9 +119,9 @@ required_ruby_version: !ruby/object:Gem::Requirement
|
|
85
119
|
version: '0'
|
86
120
|
required_rubygems_version: !ruby/object:Gem::Requirement
|
87
121
|
requirements:
|
88
|
-
- - "
|
122
|
+
- - ">"
|
89
123
|
- !ruby/object:Gem::Version
|
90
|
-
version:
|
124
|
+
version: 1.3.1
|
91
125
|
requirements: []
|
92
126
|
rubyforge_project:
|
93
127
|
rubygems_version: 2.5.1
|