devise_can 1.0.0 → 1.0.1
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 +192 -3
- data/devise_can.gemspec +1 -1
- data/lib/devise_can/version.rb +1 -1
- metadata +2 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 22a34ac3b6cb874e6ca8209a066f48f48119cd7a
|
4
|
+
data.tar.gz: 8ae3c0f0d99ca238d3587e4121c68520e366f4fc
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: eb7805d06a2b115d63f087222333a2c54e2d1a0737ec73e9bb02aa1712e8164902574b823d7e061a60245f547063c9d0022316bab93b90ff97241f766ba1dc76
|
7
|
+
data.tar.gz: dbf135e81334f2e8d464e006e53406cc660d6301d0e71cf7ee193f8c266670e9a08f61a665ceccff06ba15b23c21fb4299bb59601d3b2f77baff0c4af81e4dd8
|
data/README.md
CHANGED
@@ -1,8 +1,8 @@
|
|
1
1
|
# DeviseCan
|
2
2
|
|
3
|
-
|
3
|
+
## Why **devise_can** gem?
|
4
4
|
|
5
|
-
|
5
|
+
By `device_can` gem you'll get functionality of devise,cancancan and `user_role_association` that make your development easy.
|
6
6
|
|
7
7
|
## Installation
|
8
8
|
|
@@ -22,7 +22,196 @@ Or install it yourself as:
|
|
22
22
|
|
23
23
|
## Usage
|
24
24
|
|
25
|
-
|
25
|
+
You need to follow all given steps i.e.
|
26
|
+
|
27
|
+
## Generate devise component
|
28
|
+
|
29
|
+
`rails generate devise:install`
|
30
|
+
|
31
|
+
|
32
|
+
Now add `require 'devise'` to your config/application.rb
|
33
|
+
|
34
|
+
|
35
|
+
|
36
|
+
`rails generate devise user`
|
37
|
+
|
38
|
+
|
39
|
+
This will generate devise model with name `User`, you can either generate views and controller as well by following command:
|
40
|
+
|
41
|
+
`rails generate devise:views`
|
42
|
+
|
43
|
+
|
44
|
+
`rails generate devise:controllers users`
|
45
|
+
|
46
|
+
|
47
|
+
|
48
|
+
## Generate association component
|
49
|
+
|
50
|
+
`rails generate association user_role`
|
51
|
+
|
52
|
+
It'll generate two models with name `Role` and `UserRole`
|
53
|
+
|
54
|
+
```
|
55
|
+
class Role < ActiveRecord::Base
|
56
|
+
has_many :user_roles
|
57
|
+
has_many :users,through: :user_roles
|
58
|
+
|
59
|
+
validates :name,presence: true
|
60
|
+
end
|
61
|
+
|
62
|
+
```
|
63
|
+
|
64
|
+
|
65
|
+
```
|
66
|
+
class UserRole < ActiveRecord::Base
|
67
|
+
belongs_to :user
|
68
|
+
belongs_to :role
|
69
|
+
validates :user,presence: true
|
70
|
+
validates :role,presence: true
|
71
|
+
validates_uniqueness_of :user_id, scope: :role_id
|
72
|
+
end
|
73
|
+
|
74
|
+
```
|
75
|
+
|
76
|
+
Add association into model `User` i.e.
|
77
|
+
|
78
|
+
```
|
79
|
+
has_many :user_roles
|
80
|
+
has_many :roles,through: :user_roles
|
81
|
+
|
82
|
+
```
|
83
|
+
|
84
|
+
|
85
|
+
|
86
|
+
## Generate cancan component
|
87
|
+
|
88
|
+
`rails generate cancan:ability`
|
89
|
+
|
90
|
+
It'll create a model with name `Ability`
|
91
|
+
|
92
|
+
|
93
|
+
## Generate permission component
|
94
|
+
|
95
|
+
`rails generate permission module`
|
96
|
+
|
97
|
+
It'll generate three models with name `ModuleAction` , `ModuleGroup` and `ModulePermission`.Each model looks like:
|
98
|
+
|
99
|
+
```
|
100
|
+
|
101
|
+
class ModuleAction < ApplicationRecord
|
102
|
+
has_many :module_permissions
|
103
|
+
has_many :module_groups,through: :module_permissions
|
104
|
+
end
|
105
|
+
|
106
|
+
```
|
107
|
+
|
108
|
+
This model will contain all module name like `User` or `Role` etc..
|
109
|
+
|
110
|
+
```
|
111
|
+
|
112
|
+
class ModuleGroup < ApplicationRecord
|
113
|
+
|
114
|
+
has_many :module_permissions
|
115
|
+
has_many :module_actions,through: :module_permissions
|
116
|
+
|
117
|
+
end
|
118
|
+
|
119
|
+
```
|
120
|
+
|
121
|
+
This model will contain all actions for controller like `create/new/index/update/show` etc..
|
122
|
+
|
123
|
+
```
|
124
|
+
|
125
|
+
class ModulePermission < ApplicationRecord
|
126
|
+
|
127
|
+
belongs_to :module_group
|
128
|
+
belongs_to :module_action
|
129
|
+
belongs_to :role
|
130
|
+
|
131
|
+
end
|
132
|
+
|
133
|
+
```
|
134
|
+
|
135
|
+
This model will contain association between `module_group` , `module_action` and `role`.
|
136
|
+
|
137
|
+
|
138
|
+
Now your ability.rb file should look like:
|
139
|
+
|
140
|
+
```
|
141
|
+
|
142
|
+
class Ability
|
143
|
+
include CanCan::Ability
|
144
|
+
|
145
|
+
def initialize(user)
|
146
|
+
# Define abilities for the passed in user here. For example:
|
147
|
+
#
|
148
|
+
# user ||= User.new # guest user (not logged in)
|
149
|
+
# if user.admin?
|
150
|
+
# can :manage, :all
|
151
|
+
# else
|
152
|
+
# can :read, :all
|
153
|
+
# end
|
154
|
+
#
|
155
|
+
# The first argument to `can` is the action you are giving the user
|
156
|
+
# permission to do.
|
157
|
+
# If you pass :manage it will apply to every action. Other common actions
|
158
|
+
# here are :read, :create, :update and :destroy.
|
159
|
+
#
|
160
|
+
# The second argument is the resource the user can perform the action on.
|
161
|
+
# If you pass :all it will apply to every resource. Otherwise pass a Ruby
|
162
|
+
# class of the resource.
|
163
|
+
#
|
164
|
+
# The third argument is an optional hash of conditions to further filter the
|
165
|
+
# objects.
|
166
|
+
# For example, here the user can only update published articles.
|
167
|
+
#
|
168
|
+
# can :update, Article, :published => true
|
169
|
+
#
|
170
|
+
# See the wiki for details:
|
171
|
+
# https://github.com/CanCanCommunity/cancancan/wiki/Defining-Abilities
|
172
|
+
|
173
|
+
if user.roles.pluck(:name).include?('superadmin')
|
174
|
+
can :manage, :all
|
175
|
+
else
|
176
|
+
user.roles.map{|role| role.module_permissions}.each do |permissions|
|
177
|
+
permissions.each do |permission|
|
178
|
+
can permission.module_action.name.to_sym,permission.module_group.name.constantize
|
179
|
+
end
|
180
|
+
end
|
181
|
+
end
|
182
|
+
|
183
|
+
|
184
|
+
|
185
|
+
|
186
|
+
end
|
187
|
+
end
|
188
|
+
|
189
|
+
|
190
|
+
```
|
191
|
+
|
192
|
+
Here superadmin is a role with all permissions on each module.
|
193
|
+
|
194
|
+
## Run migrations
|
195
|
+
|
196
|
+
Now you need to run migrations for schema update
|
197
|
+
|
198
|
+
`rails db:migrate`
|
199
|
+
|
200
|
+
## Other stuffs
|
201
|
+
|
202
|
+
Add following lines into your application controller:
|
203
|
+
|
204
|
+
```
|
205
|
+
|
206
|
+
rescue_from CanCan::AccessDenied do |exception|
|
207
|
+
flash[:warning] = exception.message
|
208
|
+
redirect_to root_path
|
209
|
+
end
|
210
|
+
|
211
|
+
```
|
212
|
+
It'll check ability.rb on each action and reirect to `root_path` if found unpermitted action.
|
213
|
+
|
214
|
+
|
26
215
|
|
27
216
|
## Development
|
28
217
|
|
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{
|
13
|
+
spec.description = %q{Integrate devise,cancancan and user roles together for easy implementation.}
|
14
14
|
spec.homepage = "https://github.com/RaghavVishnoi/devise_can.git"
|
15
15
|
spec.license = "MIT"
|
16
16
|
|
data/lib/devise_can/version.rb
CHANGED
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: 1.0.
|
4
|
+
version: 1.0.1
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- RaghavSingh
|
@@ -66,7 +66,7 @@ dependencies:
|
|
66
66
|
- - ">="
|
67
67
|
- !ruby/object:Gem::Version
|
68
68
|
version: '0'
|
69
|
-
description:
|
69
|
+
description: Integrate devise,cancancan and user roles together for easy implementation.
|
70
70
|
email:
|
71
71
|
- Raghav.Singh@lptpl.com
|
72
72
|
executables: []
|