challah 0.5.3 → 0.5.4
Sign up to get free protection for your applications and to get access to all the features.
- data/CHANGELOG.md +4 -0
- data/db/seeds.rb +9 -9
- data/lib/challah/authable/permission.rb +15 -13
- data/lib/challah/authable/role.rb +22 -20
- data/lib/challah/authable/user.rb +66 -62
- data/lib/challah/version.rb +1 -1
- data/lib/tasks/crud.rake +23 -16
- data/test/permission_test.rb +1 -3
- metadata +10 -10
data/CHANGELOG.md
CHANGED
data/db/seeds.rb
CHANGED
@@ -1,12 +1,12 @@
|
|
1
1
|
# Add default admin permission and role and normal user with no permissions
|
2
2
|
if Permission.count.zero? and Role.count.zero?
|
3
|
-
admin_permission = Permission.create!(:name => 'Administrator', :key => 'admin', :description => 'Administrative users have unrestricted access to all components within the application.', :locked => true)
|
4
|
-
manage_users_permission = Permission.create!(:name => 'Manage Users', :key => 'manage_users', :description => 'Access to add, edit and remove application users.', :locked => true)
|
5
|
-
|
6
|
-
admin_role = Role.create!(:name => 'Administrator', :description => 'Administrative users have unrestricted access to all components within the application.', :default_path => '/', :locked => true)
|
7
|
-
|
8
|
-
PermissionRole.create!(:role_id => admin_role.id, :permission_id => admin_permission.id)
|
9
|
-
PermissionRole.create!(:role_id => admin_role.id, :permission_id => manage_users_permission.id)
|
10
|
-
|
11
|
-
normal_role = Role.create!(:name => 'Default', :description => 'Default users can log in to the application.', :default_path => '/')
|
3
|
+
admin_permission = Permission.create!({ :name => 'Administrator', :key => 'admin', :description => 'Administrative users have unrestricted access to all components within the application.', :locked => true }, :without_protection => true)
|
4
|
+
manage_users_permission = Permission.create!({ :name => 'Manage Users', :key => 'manage_users', :description => 'Access to add, edit and remove application users.', :locked => true }, :without_protection => true)
|
5
|
+
|
6
|
+
admin_role = Role.create!({ :name => 'Administrator', :description => 'Administrative users have unrestricted access to all components within the application.', :default_path => '/', :locked => true }, :without_protection => true)
|
7
|
+
|
8
|
+
PermissionRole.create!({ :role_id => admin_role.id, :permission_id => admin_permission.id }, :without_protection => true)
|
9
|
+
PermissionRole.create!({ :role_id => admin_role.id, :permission_id => manage_users_permission.id }, :without_protection => true)
|
10
|
+
|
11
|
+
normal_role = Role.create!({ :name => 'Default', :description => 'Default users can log in to the application.', :default_path => '/' }, :without_protection => true)
|
12
12
|
end
|
@@ -1,10 +1,10 @@
|
|
1
1
|
module Challah
|
2
|
-
# AuthablePermission is used to extend functionality to a model in your app named Permission.
|
2
|
+
# AuthablePermission is used to extend functionality to a model in your app named Permission.
|
3
3
|
# By default, this model already exists within the challah engine.
|
4
4
|
#
|
5
5
|
# The Permission model is used to store every granular level of restriction for your application.
|
6
6
|
# If there is anything within your app that may need to be restricted in any way, you'll likely
|
7
|
-
# want to create a permission for it.
|
7
|
+
# want to create a permission for it.
|
8
8
|
#
|
9
9
|
# Permission can be as granular as necessary. For example, you may have a permission called
|
10
10
|
# +:people_admin+. Or, you could specify each action taken within an admin section, and add permissions
|
@@ -25,7 +25,7 @@ module Challah
|
|
25
25
|
#
|
26
26
|
# The join tables (permission_roles and permission_users) are also included, but likely do not
|
27
27
|
# need to be accessed directly.
|
28
|
-
#
|
28
|
+
#
|
29
29
|
# == Scopes
|
30
30
|
#
|
31
31
|
# By default, the following scopes are included for this model:
|
@@ -34,10 +34,10 @@ module Challah
|
|
34
34
|
#
|
35
35
|
# == Customizing the Permission model
|
36
36
|
#
|
37
|
-
# By default, the Permission model is included within the gem engine. However, if you wish to
|
38
|
-
# include it within your app for any customizations, you can do so by creating a model
|
37
|
+
# By default, the Permission model is included within the gem engine. However, if you wish to
|
38
|
+
# include it within your app for any customizations, you can do so by creating a model
|
39
39
|
# file named +permission.rb+ and adding the +authable_permission+ line near the top of the class.
|
40
|
-
#
|
40
|
+
#
|
41
41
|
# @example app/models/permission.rb
|
42
42
|
# class Permission < ActiveRecord::Base
|
43
43
|
# # Set up all permission methods from challah gem
|
@@ -57,7 +57,7 @@ module Challah
|
|
57
57
|
include InstanceMethods
|
58
58
|
extend ClassMethods
|
59
59
|
end
|
60
|
-
|
60
|
+
|
61
61
|
class_eval do
|
62
62
|
validates_presence_of :name, :key
|
63
63
|
validates_uniqueness_of :name, :key
|
@@ -69,11 +69,13 @@ module Challah
|
|
69
69
|
has_many :users, :through => :permission_users, :order => 'users.last_name, users.first_name'
|
70
70
|
|
71
71
|
default_scope order('permissions.name')
|
72
|
-
|
72
|
+
|
73
|
+
attr_accessible :name, :description, :key, :locked
|
74
|
+
|
73
75
|
after_create :add_to_admin_role
|
74
76
|
end
|
75
77
|
end
|
76
|
-
|
78
|
+
|
77
79
|
module ClassMethods
|
78
80
|
# Quickly access a +Permission+ instance by the provided key. If no +Permission+
|
79
81
|
# is found with that key, +nil+ is returned.
|
@@ -89,8 +91,8 @@ module Challah
|
|
89
91
|
self.find_by_key(key.to_s.strip.downcase.gsub(' ', '_'))
|
90
92
|
end
|
91
93
|
end
|
92
|
-
|
93
|
-
# @private
|
94
|
+
|
95
|
+
# @private
|
94
96
|
module InstanceMethods
|
95
97
|
# @private
|
96
98
|
#
|
@@ -98,13 +100,13 @@ module Challah
|
|
98
100
|
def key=(value)
|
99
101
|
write_attribute(:key, value.to_s.downcase.strip)
|
100
102
|
end
|
101
|
-
|
103
|
+
|
102
104
|
protected
|
103
105
|
# @private
|
104
106
|
# After a new permission level is added, automatically add it to the admin user role
|
105
107
|
def add_to_admin_role
|
106
108
|
admin_role = ::Role.admin
|
107
|
-
|
109
|
+
|
108
110
|
# if there is an admin role, add this permission to it.
|
109
111
|
if admin_role
|
110
112
|
admin_role.permission_keys = admin_role.permission_keys + [ self.key ]
|
@@ -1,11 +1,11 @@
|
|
1
1
|
module Challah
|
2
|
-
# AuthableRole is used to extend functionality to a model in your app named Role. By default,
|
2
|
+
# AuthableRole is used to extend functionality to a model in your app named Role. By default,
|
3
3
|
# this model already exists within the challah engine.
|
4
4
|
#
|
5
5
|
# The Role model is used to group together sets of permissions that can be assigned
|
6
|
-
# to users.
|
6
|
+
# to users.
|
7
7
|
#
|
8
|
-
# Roles are not used to detect features or options for a user. Instead, you should
|
8
|
+
# Roles are not used to detect features or options for a user. Instead, you should
|
9
9
|
# always use permissions as the most granular level of detail within your app.
|
10
10
|
#
|
11
11
|
# For example, to restrict a piece of your application to a certain user, you should create
|
@@ -21,9 +21,9 @@ module Challah
|
|
21
21
|
# to the administrator role.
|
22
22
|
#
|
23
23
|
# == Validations
|
24
|
-
#
|
24
|
+
#
|
25
25
|
# A role requires that a unique name be provided.
|
26
|
-
#
|
26
|
+
#
|
27
27
|
# == Associations
|
28
28
|
#
|
29
29
|
# The following associations are set on this model by default:
|
@@ -36,14 +36,14 @@ module Challah
|
|
36
36
|
#
|
37
37
|
# == Customizing the Role model
|
38
38
|
#
|
39
|
-
# By default, the Role model is included within the gem engine. However, if you wish to
|
40
|
-
# include it within your app for any customizations, you can do so by creating a model
|
39
|
+
# By default, the Role model is included within the gem engine. However, if you wish to
|
40
|
+
# include it within your app for any customizations, you can do so by creating a model
|
41
41
|
# file named +role.rb+ and adding the +authable_role+ line near the top of the class.
|
42
|
-
#
|
42
|
+
#
|
43
43
|
# @example app/models/role.rb
|
44
44
|
# class Role < ActiveRecord::Base
|
45
45
|
# # Set up all role methods from challah gem
|
46
|
-
# authable_role
|
46
|
+
# authable_role
|
47
47
|
#
|
48
48
|
# # Your customizations here..
|
49
49
|
# end
|
@@ -59,21 +59,23 @@ module Challah
|
|
59
59
|
include InstanceMethods
|
60
60
|
extend ClassMethods
|
61
61
|
end
|
62
|
-
|
62
|
+
|
63
63
|
class_eval do
|
64
64
|
validates_presence_of :name
|
65
65
|
validates_uniqueness_of :name
|
66
|
-
|
66
|
+
|
67
67
|
has_many :users, :order => 'users.first_name, users.last_name'
|
68
68
|
has_many :permission_roles, :dependent => :destroy
|
69
69
|
has_many :permissions, :through => :permission_roles, :order => 'permissions.name'
|
70
|
-
|
70
|
+
|
71
71
|
default_scope order('roles.name')
|
72
72
|
|
73
|
+
attr_accessible :name, :description, :default_path, :locked
|
74
|
+
|
73
75
|
after_save :save_permission_keys
|
74
76
|
end
|
75
77
|
end
|
76
|
-
|
78
|
+
|
77
79
|
module ClassMethods
|
78
80
|
# Quickly access a +Role+ instance by the provided name. If no +Role+
|
79
81
|
# is found with that key, +nil+ is returned.
|
@@ -88,7 +90,7 @@ module Challah
|
|
88
90
|
def [](name)
|
89
91
|
self.find_by_name(name.to_s.strip.downcase.gsub(' ', '_').titleize)
|
90
92
|
end
|
91
|
-
|
93
|
+
|
92
94
|
# Shortcut for finding the Role named 'Administrator'
|
93
95
|
#
|
94
96
|
# @return [Role, nil]
|
@@ -99,7 +101,7 @@ module Challah
|
|
99
101
|
@admin ||= self.find_by_name('Administrator')
|
100
102
|
end
|
101
103
|
end
|
102
|
-
|
104
|
+
|
103
105
|
module InstanceMethods
|
104
106
|
# Grab all permission keys for this +Role+
|
105
107
|
#
|
@@ -113,7 +115,7 @@ module Challah
|
|
113
115
|
end
|
114
116
|
|
115
117
|
# Set the permission keys that this role can access. This temporarily updates
|
116
|
-
# the permission keys for the +Role+ instance, but changes are not saved until
|
118
|
+
# the permission keys for the +Role+ instance, but changes are not saved until
|
117
119
|
# the model has been saved.
|
118
120
|
#
|
119
121
|
# @param [Array] keys An array of permission keys to set for this role.
|
@@ -124,7 +126,7 @@ module Challah
|
|
124
126
|
@permission_keys = keys
|
125
127
|
@permission_keys
|
126
128
|
end
|
127
|
-
|
129
|
+
|
128
130
|
# Does this role have the given +Permission+? Pass in a Permission instance, or
|
129
131
|
# a permission key to check for its existance.
|
130
132
|
#
|
@@ -150,7 +152,7 @@ module Challah
|
|
150
152
|
return has(sym.to_s.gsub(/\?/, '')) if sym.to_s =~ /^[a-z0-9_]*\?$/
|
151
153
|
super(sym, *args, &block)
|
152
154
|
end
|
153
|
-
|
155
|
+
|
154
156
|
protected
|
155
157
|
# @private
|
156
158
|
#
|
@@ -161,9 +163,9 @@ module Challah
|
|
161
163
|
|
162
164
|
@permission_keys.uniq.each do |key|
|
163
165
|
permission = ::Permission.find_by_key(key)
|
164
|
-
|
166
|
+
|
165
167
|
if permission
|
166
|
-
self.permission_roles.create(:permission_id => permission.id, :role_id => self.id)
|
168
|
+
self.permission_roles.create({ :permission_id => permission.id, :role_id => self.id }, :without_protection => true)
|
167
169
|
end
|
168
170
|
end
|
169
171
|
|
@@ -5,107 +5,109 @@ module Challah
|
|
5
5
|
include InstanceMethods
|
6
6
|
extend ClassMethods
|
7
7
|
end
|
8
|
-
|
8
|
+
|
9
9
|
class_eval do
|
10
10
|
cattr_accessor :protected_attributes
|
11
|
-
|
11
|
+
|
12
12
|
validates_presence_of :first_name, :last_name, :email, :role_id, :username
|
13
13
|
validates_uniqueness_of :email, :username
|
14
14
|
validate :validate_new_password
|
15
|
-
|
15
|
+
|
16
16
|
before_save :before_save_password
|
17
|
-
|
17
|
+
|
18
18
|
belongs_to :role, :touch => true
|
19
19
|
has_many :permission_users, :dependent => :destroy
|
20
20
|
has_many :permissions, :through => :permission_users, :order => 'permissions.name'
|
21
|
-
|
21
|
+
|
22
22
|
scope :active, where(:active => true).order('users.first_name, users.last_name')
|
23
23
|
scope :inactive, where(:active => false).order('users.first_name, users.last_name')
|
24
24
|
scope :with_role, lambda { |role| where([ "users.role_id = ?", role ]) }
|
25
25
|
scope :search, lambda { |q| where([ 'users.first_name like ? OR users.last_name like ? OR users.email like ? OR users.username LIKE ?', "%#{q}%", "%#{q}%", "%#{q}%", "%#{q}%" ]) }
|
26
26
|
after_save :save_permission_keys
|
27
|
-
|
28
|
-
|
27
|
+
|
28
|
+
attr_accessible :first_name, :last_name, :username, :email, :password, :password_confirmation
|
29
|
+
|
30
|
+
protect_attributes :api_key, :created_by, :crypted_password, :failed_login_count, :id, :last_session_at, :last_login_at, :last_session_ip, :login_count, :permissions, :permissions_attributes, :permission_users, :permission_users_attributes, :persistence_token, :role_id, :session_count, :updated_by
|
29
31
|
end
|
30
32
|
end
|
31
|
-
|
33
|
+
|
32
34
|
module ClassMethods
|
33
35
|
# Find a user instance by username first, or email address if needed.
|
34
36
|
# If no user is found matching, return nil
|
35
37
|
def find_for_session(username_or_email)
|
36
38
|
return nil if username_or_email.to_s.blank?
|
37
|
-
|
39
|
+
|
38
40
|
result = nil
|
39
|
-
|
41
|
+
|
40
42
|
result = find_by_username(username_or_email)
|
41
|
-
|
43
|
+
|
42
44
|
unless result
|
43
45
|
if username_or_email.to_s.include?('@')
|
44
46
|
result = find_by_email(username_or_email)
|
45
|
-
end
|
46
|
-
end
|
47
|
-
|
47
|
+
end
|
48
|
+
end
|
49
|
+
|
48
50
|
result
|
49
51
|
end
|
50
|
-
|
51
|
-
def protect_attributes(*args)
|
52
|
+
|
53
|
+
def protect_attributes(*args)
|
52
54
|
self.protected_attributes ||= []
|
53
55
|
self.protected_attributes << args.collect(&:to_s)
|
54
56
|
end
|
55
57
|
end
|
56
|
-
|
58
|
+
|
57
59
|
# Instance methods to be included once authable_user is set up.
|
58
60
|
module InstanceMethods
|
59
|
-
# Returns true if this user is active, and should be able to log in. If
|
61
|
+
# Returns true if this user is active, and should be able to log in. If
|
60
62
|
# the active column is false, the user will not be able to authenticate
|
61
63
|
def active?
|
62
64
|
!!self.active
|
63
65
|
end
|
64
|
-
|
66
|
+
|
65
67
|
# Generic authentication method. By default, this just checks to see if the password
|
66
|
-
# given matches this user. You can also pass in the first parameter as the method
|
68
|
+
# given matches this user. You can also pass in the first parameter as the method
|
67
69
|
# to use for a different type of authentication.
|
68
70
|
def authenticate(*args)
|
69
71
|
return false unless active?
|
70
|
-
|
72
|
+
|
71
73
|
if args.length > 1
|
72
74
|
method = args.shift
|
73
|
-
|
75
|
+
|
74
76
|
if respond_to?("authenticate_with_#{method}")
|
75
77
|
return self.send("authenticate_with_#{method}", *args)
|
76
78
|
end
|
77
|
-
|
78
|
-
false
|
79
|
+
|
80
|
+
false
|
79
81
|
else
|
80
82
|
authenticate_with_password(args[0])
|
81
83
|
end
|
82
84
|
end
|
83
|
-
|
85
|
+
|
84
86
|
# Pass in an api_key, and if it matches this user account, return true.
|
85
87
|
def authenticate_with_api_key(api_key)
|
86
88
|
self.api_key == api_key
|
87
89
|
end
|
88
|
-
|
90
|
+
|
89
91
|
# Pass in a password, and if it matches this user's account, return true.
|
90
92
|
def authenticate_with_password(plain_password)
|
91
93
|
::Challah::Encrypter.compare(self.crypted_password, plain_password)
|
92
94
|
end
|
93
|
-
|
95
|
+
|
94
96
|
# The default url where this user should be redirected to after logging in. Also can be used as the main link
|
95
97
|
# at the top of navigation.
|
96
98
|
def default_path
|
97
99
|
role ? role.default_path : '/'
|
98
100
|
end
|
99
|
-
|
101
|
+
|
100
102
|
def failed_authentication!
|
101
103
|
self.increment!(:failed_auth_count)
|
102
104
|
end
|
103
|
-
|
105
|
+
|
104
106
|
# full name
|
105
107
|
def name
|
106
108
|
"#{first_name} #{last_name}"
|
107
109
|
end
|
108
|
-
|
110
|
+
|
109
111
|
# Get the value of the current password, only can be used right after setting a new password.
|
110
112
|
def password
|
111
113
|
@password
|
@@ -126,37 +128,37 @@ module Challah
|
|
126
128
|
def password_confirmation=(value)
|
127
129
|
@password_confirmation = value
|
128
130
|
end
|
129
|
-
|
131
|
+
|
130
132
|
# Returns the permission keys in an array for exactly what this user can access. This includes all role based permission keys, and any specifically given to this user through permissions_users
|
131
133
|
def permission_keys
|
132
134
|
return @permission_keys if @permission_keys
|
133
|
-
|
135
|
+
|
134
136
|
role_keys = if role(true)
|
135
137
|
role_key = "#{role.cache_key}/permissions"
|
136
|
-
|
138
|
+
|
137
139
|
keys = Rails.cache.fetch(role_key) do
|
138
140
|
role.permission_keys.clone
|
139
141
|
end
|
140
|
-
|
141
|
-
Rails.cache.write(role_key, keys)
|
142
|
+
|
143
|
+
Rails.cache.write(role_key, keys)
|
142
144
|
keys
|
143
145
|
else
|
144
146
|
[]
|
145
147
|
end
|
146
|
-
|
148
|
+
|
147
149
|
user_key = "#{self.cache_key}/permissions"
|
148
|
-
|
150
|
+
|
149
151
|
user_keys = Rails.cache.fetch(user_key) do
|
150
152
|
user_permission_keys.clone
|
151
153
|
end
|
152
|
-
|
154
|
+
|
153
155
|
user_keys = [] unless user_keys
|
154
|
-
|
156
|
+
|
155
157
|
Rails.cache.write(user_key, keys) unless new_record?
|
156
158
|
|
157
|
-
@permission_keys = (role_keys + user_keys).uniq
|
159
|
+
@permission_keys = (role_keys + user_keys).uniq
|
158
160
|
end
|
159
|
-
|
161
|
+
|
160
162
|
# Returns true if this user has permission to the provided permission key
|
161
163
|
def has(permission_key)
|
162
164
|
self.permission_keys.include?(permission_key.to_s)
|
@@ -166,46 +168,48 @@ module Challah
|
|
166
168
|
# Set the permission keys that this role can access
|
167
169
|
def permission_keys=(value)
|
168
170
|
Rails.cache.delete("#{self.cache_key}/permissions")
|
169
|
-
|
171
|
+
|
170
172
|
@permission_keys = value
|
171
173
|
@permission_keys
|
172
174
|
end
|
173
|
-
|
175
|
+
|
174
176
|
# When a role is set, reset the permission_keys
|
175
177
|
def role_id=(value)
|
176
178
|
@permission_keys = nil
|
177
179
|
@user_permission_keys = nil
|
178
|
-
|
180
|
+
|
179
181
|
self[:role_id] = value
|
180
182
|
end
|
181
|
-
|
183
|
+
|
182
184
|
# shortened name, just includes the first name and last initial
|
183
185
|
def small_name
|
184
186
|
"#{first_name.to_s.titleize} #{last_name.to_s.first.upcase}."
|
185
187
|
end
|
186
|
-
|
187
|
-
# Called when a +Session+ validation is successful, and this user has
|
188
|
+
|
189
|
+
# Called when a +Session+ validation is successful, and this user has
|
188
190
|
# been authenticated.
|
189
191
|
def successful_authentication!(ip_address = nil)
|
190
|
-
self.
|
192
|
+
self.last_session_at = Time.now
|
193
|
+
self.last_session_ip = ip_address
|
194
|
+
self.save
|
191
195
|
self.increment!(:session_count, 1)
|
192
196
|
end
|
193
|
-
|
197
|
+
|
194
198
|
# Update a user's own account. This differsfrom User#update_attributes because it won't let
|
195
|
-
# a user update their own role and other protected elements.
|
199
|
+
# a user update their own role and other protected elements.
|
196
200
|
#
|
197
201
|
# All attributes on the user model can be updated, except for the ones listed below.
|
198
202
|
def update_account_attributes(attributes_to_update = {})
|
199
|
-
protected_attributes = self.class.protected_attributes.clone.flatten
|
200
|
-
attributes_to_update.keys.each { |key| attributes_to_update.delete(key) if protected_attributes.include?(key.to_s) }
|
203
|
+
protected_attributes = self.class.protected_attributes.clone.flatten
|
204
|
+
attributes_to_update.keys.each { |key| attributes_to_update.delete(key) if protected_attributes.include?(key.to_s) }
|
201
205
|
self.update_attributes(attributes_to_update)
|
202
206
|
end
|
203
|
-
|
204
|
-
# Returns the permission keys used by this specific user, does not include any role-based permissions.
|
207
|
+
|
208
|
+
# Returns the permission keys used by this specific user, does not include any role-based permissions.
|
205
209
|
def user_permission_keys
|
206
210
|
new_record? ? [] : self.permissions(true).collect(&:key)
|
207
211
|
end
|
208
|
-
|
212
|
+
|
209
213
|
# Is this user valid and ready for a user session?
|
210
214
|
#
|
211
215
|
# Override this method if you need to check for a particular configuration on each page request.
|
@@ -214,7 +218,7 @@ module Challah
|
|
214
218
|
end
|
215
219
|
|
216
220
|
# Allow dynamic checking for permissions
|
217
|
-
#
|
221
|
+
#
|
218
222
|
# +admin?+ is shorthand for:
|
219
223
|
#
|
220
224
|
# def admin?
|
@@ -224,7 +228,7 @@ module Challah
|
|
224
228
|
return has(sym.to_s.gsub(/\?/, '')) if sym.to_s =~ /^[a-z_]*\?$/
|
225
229
|
super(sym, *args, &block)
|
226
230
|
end
|
227
|
-
|
231
|
+
|
228
232
|
protected
|
229
233
|
# called before_save on the User model, actually encrypts the password with a new generated salt
|
230
234
|
def before_save_password
|
@@ -238,10 +242,10 @@ module Challah
|
|
238
242
|
self.persistence_token = ::Challah::Random.token(125) if self.persistence_token.to_s.blank?
|
239
243
|
self.api_key = ::Challah::Random.token(50) if self.api_key.to_s.blank?
|
240
244
|
end
|
241
|
-
|
242
|
-
# Saves any updated permission keys to the database for this user.
|
243
|
-
# Any permission keys that are specifically given to this user and are also in the
|
244
|
-
# user's role will be removed. So, the only permission keys added here will be those
|
245
|
+
|
246
|
+
# Saves any updated permission keys to the database for this user.
|
247
|
+
# Any permission keys that are specifically given to this user and are also in the
|
248
|
+
# user's role will be removed. So, the only permission keys added here will be those
|
245
249
|
# in addition to the user's role.
|
246
250
|
def save_permission_keys
|
247
251
|
if @permission_keys and Array === @permission_keys
|
@@ -253,7 +257,7 @@ module Challah
|
|
253
257
|
permission = ::Permission[key]
|
254
258
|
|
255
259
|
if permission
|
256
|
-
self.permission_users.create(:permission_id => permission.id, :user_id => self.id)
|
260
|
+
self.permission_users.create({ :permission_id => permission.id, :user_id => self.id }, :without_protection => true)
|
257
261
|
end
|
258
262
|
end
|
259
263
|
|
data/lib/challah/version.rb
CHANGED
data/lib/tasks/crud.rake
CHANGED
@@ -1,6 +1,6 @@
|
|
1
1
|
require 'highline/import'
|
2
2
|
|
3
|
-
namespace :challah do
|
3
|
+
namespace :challah do
|
4
4
|
namespace :permissions do
|
5
5
|
desc "Create a new permission"
|
6
6
|
task :create => :environment do
|
@@ -9,25 +9,25 @@ namespace :challah do
|
|
9
9
|
|
10
10
|
banner('Creating a permission')
|
11
11
|
|
12
|
-
# Grab the required fields.
|
12
|
+
# Grab the required fields.
|
13
13
|
name = ask('Permission name: ')
|
14
14
|
key = name.to_s.parameterize.underscore
|
15
|
-
key = ask('Key: ') { |q| q.default = key }
|
15
|
+
key = ask('Key: ') { |q| q.default = key }
|
16
16
|
description = ask('Description (optional): ')
|
17
|
-
|
18
|
-
permission = Permission.new(:name => name, :key => key, :description => description)
|
17
|
+
|
18
|
+
permission = Permission.new({ :name => name, :key => key, :description => description }, :without_protection => true)
|
19
19
|
|
20
20
|
puts "\n"
|
21
21
|
|
22
22
|
if permission.save
|
23
|
-
puts "
|
23
|
+
puts "Permission has been created successfully! [ID: #{permission.id}]"
|
24
24
|
else
|
25
|
-
puts "
|
25
|
+
puts "Permission could not be added for the following errors:"
|
26
26
|
permission.errors.full_messages.each { |m| puts " - #{m}" }
|
27
27
|
end
|
28
28
|
end
|
29
29
|
end
|
30
|
-
|
30
|
+
|
31
31
|
namespace :roles do
|
32
32
|
desc "Create a new role"
|
33
33
|
task :create => :environment do
|
@@ -36,11 +36,11 @@ namespace :challah do
|
|
36
36
|
|
37
37
|
banner('Creating a role')
|
38
38
|
|
39
|
-
# Grab the required fields.
|
39
|
+
# Grab the required fields.
|
40
40
|
name = ask('Name: ')
|
41
41
|
description = ask('Description (optional): ')
|
42
|
-
|
43
|
-
role = Role.new(:name => name, :description => description)
|
42
|
+
|
43
|
+
role = Role.new({ :name => name, :description => description }, :without_protection => true)
|
44
44
|
|
45
45
|
puts "\n"
|
46
46
|
|
@@ -52,7 +52,7 @@ namespace :challah do
|
|
52
52
|
end
|
53
53
|
end
|
54
54
|
end
|
55
|
-
|
55
|
+
|
56
56
|
namespace :users do
|
57
57
|
desc "Create a new user"
|
58
58
|
task :create => :environment do
|
@@ -65,9 +65,9 @@ namespace :challah do
|
|
65
65
|
|
66
66
|
if first_user
|
67
67
|
puts "Please answer the following questions to create your first admin user.\n\n"
|
68
|
-
end
|
68
|
+
end
|
69
69
|
|
70
|
-
# Grab the required fields.
|
70
|
+
# Grab the required fields.
|
71
71
|
first_name = ask('First name: ')
|
72
72
|
last_name = ask('Last name: ')
|
73
73
|
email = ask('Email: ')
|
@@ -81,14 +81,21 @@ namespace :challah do
|
|
81
81
|
unless first_user
|
82
82
|
choose do |menu|
|
83
83
|
menu.prompt = 'Choose a role for this user: '
|
84
|
-
|
84
|
+
|
85
85
|
Role.all.each do |role|
|
86
86
|
menu.choice(role.name) { role_id = role.id }
|
87
87
|
end
|
88
88
|
end
|
89
89
|
end
|
90
90
|
|
91
|
-
user = User.new(
|
91
|
+
user = User.new({
|
92
|
+
:first_name => first_name,
|
93
|
+
:last_name => last_name,
|
94
|
+
:email => email,
|
95
|
+
:username => username,
|
96
|
+
:role_id => role_id,
|
97
|
+
:password => password,
|
98
|
+
:password_confirmation => password }, :without_protection => true)
|
92
99
|
|
93
100
|
puts "\n"
|
94
101
|
|
data/test/permission_test.rb
CHANGED
@@ -36,9 +36,7 @@ class PermissionTest < ActiveSupport::TestCase
|
|
36
36
|
admin_role = create(:role, :name => 'Administrator')
|
37
37
|
assert_equal nil, admin_role.permission_keys.index('new_permission')
|
38
38
|
|
39
|
-
|
40
|
-
|
41
|
-
permission = Permission.new(:name => 'New Permission', :key => 'new_permission', :description => 'This is just a test.')
|
39
|
+
permission = Permission.new({ :name => 'New Permission', :key => 'new_permission', :description => 'This is just a test.' }, :without_protection => true)
|
42
40
|
|
43
41
|
assert_difference [ 'Permission.count', 'PermissionRole.count' ], 1 do
|
44
42
|
assert permission.save
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: challah
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.5.
|
4
|
+
version: 0.5.4
|
5
5
|
prerelease:
|
6
6
|
platform: ruby
|
7
7
|
authors:
|
@@ -9,11 +9,11 @@ authors:
|
|
9
9
|
autorequire:
|
10
10
|
bindir: bin
|
11
11
|
cert_chain: []
|
12
|
-
date: 2012-
|
12
|
+
date: 2012-04-06 00:00:00.000000000Z
|
13
13
|
dependencies:
|
14
14
|
- !ruby/object:Gem::Dependency
|
15
15
|
name: highline
|
16
|
-
requirement: &
|
16
|
+
requirement: &70222396826020 !ruby/object:Gem::Requirement
|
17
17
|
none: false
|
18
18
|
requirements:
|
19
19
|
- - ! '>='
|
@@ -21,10 +21,10 @@ dependencies:
|
|
21
21
|
version: '0'
|
22
22
|
type: :runtime
|
23
23
|
prerelease: false
|
24
|
-
version_requirements: *
|
24
|
+
version_requirements: *70222396826020
|
25
25
|
- !ruby/object:Gem::Dependency
|
26
26
|
name: rails
|
27
|
-
requirement: &
|
27
|
+
requirement: &70222397669900 !ruby/object:Gem::Requirement
|
28
28
|
none: false
|
29
29
|
requirements:
|
30
30
|
- - ! '>='
|
@@ -32,10 +32,10 @@ dependencies:
|
|
32
32
|
version: '3.1'
|
33
33
|
type: :runtime
|
34
34
|
prerelease: false
|
35
|
-
version_requirements: *
|
35
|
+
version_requirements: *70222397669900
|
36
36
|
- !ruby/object:Gem::Dependency
|
37
37
|
name: rake
|
38
|
-
requirement: &
|
38
|
+
requirement: &70222397682260 !ruby/object:Gem::Requirement
|
39
39
|
none: false
|
40
40
|
requirements:
|
41
41
|
- - ! '>='
|
@@ -43,10 +43,10 @@ dependencies:
|
|
43
43
|
version: 0.9.2
|
44
44
|
type: :runtime
|
45
45
|
prerelease: false
|
46
|
-
version_requirements: *
|
46
|
+
version_requirements: *70222397682260
|
47
47
|
- !ruby/object:Gem::Dependency
|
48
48
|
name: bcrypt-ruby
|
49
|
-
requirement: &
|
49
|
+
requirement: &70222397727080 !ruby/object:Gem::Requirement
|
50
50
|
none: false
|
51
51
|
requirements:
|
52
52
|
- - ! '>='
|
@@ -54,7 +54,7 @@ dependencies:
|
|
54
54
|
version: '0'
|
55
55
|
type: :runtime
|
56
56
|
prerelease: false
|
57
|
-
version_requirements: *
|
57
|
+
version_requirements: *70222397727080
|
58
58
|
description: A simple ruby gem for authentication, users, roles and permissions.
|
59
59
|
email:
|
60
60
|
- john@johntornow.com
|