RubyRoles 0.1.0 → 0.1.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/.idea/RubyRoles.iml +3 -0
- data/README.md +83 -9
- data/lib/RubyRoles.rb +15 -3
- data/lib/ruby_roles/acts_as_ruby_roles/class_methods.rb +14 -0
- data/lib/ruby_roles/acts_as_ruby_roles/instance_methods.rb +10 -0
- data/lib/ruby_roles/concerns/roles_mgmt.rb +20 -9
- data/lib/ruby_roles/version.rb +1 -1
- metadata +17 -1
checksums.yaml
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
---
|
|
2
2
|
SHA256:
|
|
3
|
-
metadata.gz:
|
|
4
|
-
data.tar.gz:
|
|
3
|
+
metadata.gz: 2f57af3eeac9369deaa6fd24b79a20b024d8f99a990d9f225948f5d5552f254c
|
|
4
|
+
data.tar.gz: caa589a3e7489e2c584d84d20a03f70ab925ef3e6da4eca963c8618f3cbaddb5
|
|
5
5
|
SHA512:
|
|
6
|
-
metadata.gz:
|
|
7
|
-
data.tar.gz:
|
|
6
|
+
metadata.gz: 9a987716021bc8e52f212fc44a357bb9e7c166240b690f691c7de40723ee6462a0dd5687a26d93c2d0e4663b1bbbc561d3e7c820d5e6371b048cc150e0e7e93c
|
|
7
|
+
data.tar.gz: 8cf010bd6a7f2c0cf9e6a3244dcfa812fd1196479487dd8fed62f52cdc17a3e827bd883f4a2b415c0058579168c96849bef5e9239239b923c1fc1edfa02eb3e6
|
data/.idea/RubyRoles.iml
CHANGED
|
@@ -11,6 +11,8 @@
|
|
|
11
11
|
</content>
|
|
12
12
|
<orderEntry type="inheritedJdk" />
|
|
13
13
|
<orderEntry type="sourceFolder" forTests="false" />
|
|
14
|
+
<orderEntry type="library" scope="PROVIDED" name="activemodel (v8.0.2, ruby-3.2.2-p53) [gem]" level="application" />
|
|
15
|
+
<orderEntry type="library" scope="PROVIDED" name="activerecord (v8.0.2, ruby-3.2.2-p53) [gem]" level="application" />
|
|
14
16
|
<orderEntry type="library" scope="PROVIDED" name="activesupport (v8.0.2, ruby-3.2.2-p53) [gem]" level="application" />
|
|
15
17
|
<orderEntry type="library" scope="PROVIDED" name="base64 (v0.3.0, ruby-3.2.2-p53) [gem]" level="application" />
|
|
16
18
|
<orderEntry type="library" scope="PROVIDED" name="benchmark (v0.4.1, ruby-3.2.2-p53) [gem]" level="application" />
|
|
@@ -34,6 +36,7 @@
|
|
|
34
36
|
<orderEntry type="library" scope="PROVIDED" name="reline (v0.6.2, ruby-3.2.2-p53) [gem]" level="application" />
|
|
35
37
|
<orderEntry type="library" scope="PROVIDED" name="securerandom (v0.4.1, ruby-3.2.2-p53) [gem]" level="application" />
|
|
36
38
|
<orderEntry type="library" scope="PROVIDED" name="stringio (v3.1.7, ruby-3.2.2-p53) [gem]" level="application" />
|
|
39
|
+
<orderEntry type="library" scope="PROVIDED" name="timeout (v0.4.3, ruby-3.2.2-p53) [gem]" level="application" />
|
|
37
40
|
<orderEntry type="library" scope="PROVIDED" name="tzinfo (v2.0.6, ruby-3.2.2-p53) [gem]" level="application" />
|
|
38
41
|
<orderEntry type="library" scope="PROVIDED" name="uri (v1.0.3, ruby-3.2.2-p53) [gem]" level="application" />
|
|
39
42
|
</component>
|
data/README.md
CHANGED
|
@@ -1,28 +1,95 @@
|
|
|
1
1
|
# RubyRoles
|
|
2
2
|
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
Welcome to your new gem! In this directory, you'll find the files you need to be able to package up your Ruby library into a gem. Put your Ruby code in the file `lib/RubyRoles`. To experiment with that code, run `bin/console` for an interactive prompt.
|
|
3
|
+
Assign roles to users, accounts, user_accounts, and other models in a Ruby on Rails application.
|
|
6
4
|
|
|
7
5
|
## Installation
|
|
8
6
|
|
|
9
|
-
TODO: Replace `UPDATE_WITH_YOUR_GEM_NAME_IMMEDIATELY_AFTER_RELEASE_TO_RUBYGEMS_ORG` with your gem name right after releasing it to RubyGems.org. Please do not do it earlier due to security reasons. Alternatively, replace this section with instructions to install your gem from git if you don't plan to release to RubyGems.org.
|
|
10
|
-
|
|
11
7
|
Install the gem and add to the application's Gemfile by executing:
|
|
12
8
|
|
|
13
9
|
```bash
|
|
14
|
-
bundle add
|
|
10
|
+
bundle add RubyRoles
|
|
15
11
|
```
|
|
16
12
|
|
|
17
13
|
If bundler is not being used to manage dependencies, install the gem by executing:
|
|
18
14
|
|
|
19
15
|
```bash
|
|
20
|
-
gem install
|
|
16
|
+
gem install RubyRoles
|
|
21
17
|
```
|
|
22
18
|
|
|
23
19
|
## Usage
|
|
24
20
|
|
|
25
|
-
|
|
21
|
+
```ruby
|
|
22
|
+
class MyModel < ApplicationRecord
|
|
23
|
+
acts_as_ruby_roles
|
|
24
|
+
roles :sysadmin, :syssupport # , ... << # NOTE: Never remove entries, never reorder entries, always add to the end of the list only
|
|
25
|
+
end
|
|
26
|
+
```
|
|
27
|
+
|
|
28
|
+
The default field used to store the roles is named "roles_mask", and it should be defined
|
|
29
|
+
in the ActiveRecord migration like so:
|
|
30
|
+
|
|
31
|
+
```ruby
|
|
32
|
+
def change
|
|
33
|
+
create_table :my_models do |t|
|
|
34
|
+
# ...
|
|
35
|
+
t.integer :roles_mask, default: 0, null: false
|
|
36
|
+
# ...
|
|
37
|
+
end
|
|
38
|
+
end
|
|
39
|
+
```
|
|
40
|
+
|
|
41
|
+
or, after the table was previous created:
|
|
42
|
+
|
|
43
|
+
```ruby
|
|
44
|
+
def change
|
|
45
|
+
add_column :my_models, :roles_mask, :integer, default: 0, null: false
|
|
46
|
+
end
|
|
47
|
+
```
|
|
48
|
+
|
|
49
|
+
If you want to change the name of the field, simply specify the field name in the
|
|
50
|
+
acts_as_ruby_roles call in the model:
|
|
51
|
+
|
|
52
|
+
```ruby
|
|
53
|
+
class MyModel < ApplicationRecord
|
|
54
|
+
acts_as_ruby_roles field_name: :my_alternate_roles_field_name
|
|
55
|
+
roles :sysadmin, :syssupport, :viewonly # , ... << # NOTE: Never remove entries, never reorder entries, always add to the end of the list only
|
|
56
|
+
end
|
|
57
|
+
```
|
|
58
|
+
|
|
59
|
+
Once you have this defined, you will have methods like the following available to you:
|
|
60
|
+
|
|
61
|
+
```ruby
|
|
62
|
+
user = User.first
|
|
63
|
+
user.is_sysadmin?
|
|
64
|
+
user.is_syssupport?
|
|
65
|
+
user.is_viewonly?
|
|
66
|
+
```
|
|
67
|
+
|
|
68
|
+
To set the roles, use:
|
|
69
|
+
|
|
70
|
+
```ruby
|
|
71
|
+
user = User.first
|
|
72
|
+
user.roles= [:sysadmin]
|
|
73
|
+
user.roles= [:sysadmin,:syssupport]
|
|
74
|
+
user.roles= [:sysadmin,:viewonly]
|
|
75
|
+
user.save!
|
|
76
|
+
```
|
|
77
|
+
|
|
78
|
+
And all the roles can be read by:
|
|
79
|
+
|
|
80
|
+
```ruby
|
|
81
|
+
user = User.first
|
|
82
|
+
user.roles => [:sysadmin,:syssupport,...]
|
|
83
|
+
user.save!
|
|
84
|
+
```
|
|
85
|
+
|
|
86
|
+
In the database, roles are stored as integers.
|
|
87
|
+
The values will return true or false depending on if the specified bit is set in the integer roles_mask for the model instance.
|
|
88
|
+
The bits are defined sequentially in the "roles" call. So, in this example, :sysadmin
|
|
89
|
+
is the 0th bit (integer value 1), and :syssupport is the 1st bit (integer value 2), and :viewonly
|
|
90
|
+
is the 2nd bit (integer value 4).
|
|
91
|
+
|
|
92
|
+
An instance can be multiple roles by bit-wise ORing the values together.
|
|
26
93
|
|
|
27
94
|
## Development
|
|
28
95
|
|
|
@@ -32,4 +99,11 @@ To install this gem onto your local machine, run `bundle exec rake install`. To
|
|
|
32
99
|
|
|
33
100
|
## Contributing
|
|
34
101
|
|
|
35
|
-
Bug reports and pull requests are welcome on GitHub at https://github.com/
|
|
102
|
+
Bug reports and pull requests are welcome on GitHub at https://github.com/leeatchison/RubyRoles.
|
|
103
|
+
|
|
104
|
+
To build a new gem (authorized deployers only can do the push):
|
|
105
|
+
|
|
106
|
+
```bash
|
|
107
|
+
gem build RubyRoles.gemspec
|
|
108
|
+
gem push RubyRoles-#.#.#.gem
|
|
109
|
+
```
|
data/lib/RubyRoles.rb
CHANGED
|
@@ -1,9 +1,21 @@
|
|
|
1
1
|
# frozen_string_literal: true
|
|
2
2
|
|
|
3
|
-
|
|
4
|
-
|
|
3
|
+
require 'active_support'
|
|
4
|
+
require 'active_record'
|
|
5
|
+
require 'active_support/concern'
|
|
6
|
+
require "ruby_roles/version"
|
|
7
|
+
require "ruby_roles/concerns/roles_mgmt"
|
|
8
|
+
require "ruby_roles/acts_as_ruby_roles/instance_methods"
|
|
9
|
+
require "ruby_roles/acts_as_ruby_roles/class_methods"
|
|
5
10
|
|
|
6
11
|
module RubyRoles
|
|
7
12
|
class Error < StandardError; end
|
|
8
|
-
|
|
13
|
+
|
|
14
|
+
class Railtie < Rails::Railtie
|
|
15
|
+
initializer 'RubyRoles.acts_as_ruby_roles' do
|
|
16
|
+
ActiveSupport.on_load(:active_record) do
|
|
17
|
+
ActiveRecord::Base.extend RubyRoles::ActsAsRubyRoles::ClassMethods
|
|
18
|
+
end
|
|
19
|
+
end
|
|
20
|
+
end
|
|
9
21
|
end
|
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
# lib/ruby_roles/acts_as_ruby_roles/class_methods.rb
|
|
2
|
+
module RubyRoles
|
|
3
|
+
module ActsAsRubyRoles
|
|
4
|
+
extend ActiveSupport::Concern
|
|
5
|
+
module ClassMethods
|
|
6
|
+
def acts_as_ruby_roles(options = {})
|
|
7
|
+
include RubyRoles::ActsAsRubyRoles::InstanceMethods
|
|
8
|
+
include RubyRoles::Concerns::RolesMgmt
|
|
9
|
+
# Initialize the class instance variables
|
|
10
|
+
@roles_field_name = options[:field_name] if options[:field_name].present?
|
|
11
|
+
end
|
|
12
|
+
end
|
|
13
|
+
end
|
|
14
|
+
end
|
|
@@ -3,9 +3,9 @@ module RubyRoles
|
|
|
3
3
|
module RolesMgmt
|
|
4
4
|
extend ActiveSupport::Concern
|
|
5
5
|
|
|
6
|
-
|
|
7
6
|
included do
|
|
8
7
|
@role_list = []
|
|
8
|
+
@roles_field_name = :roles_mask
|
|
9
9
|
|
|
10
10
|
#
|
|
11
11
|
#
|
|
@@ -13,6 +13,14 @@ module RubyRoles
|
|
|
13
13
|
#
|
|
14
14
|
#
|
|
15
15
|
|
|
16
|
+
# Sets the roles field name
|
|
17
|
+
def self.roles_field name
|
|
18
|
+
@roles_field_name = name || :roles_mask
|
|
19
|
+
end
|
|
20
|
+
def self.get_roles_field
|
|
21
|
+
@roles_field_name
|
|
22
|
+
end
|
|
23
|
+
|
|
16
24
|
# Sets the acceptable list of Roles
|
|
17
25
|
def self.roles(*roles_list)
|
|
18
26
|
@role_list = canonical_syms *roles_list
|
|
@@ -52,7 +60,7 @@ module RubyRoles
|
|
|
52
60
|
end
|
|
53
61
|
# Return list of human readable strings matching the roles
|
|
54
62
|
def roles_human_readable
|
|
55
|
-
self.class.roles_bits_to_syms(self.
|
|
63
|
+
self.class.roles_bits_to_syms(self.send(self.get_roles_field)).map { |role|self.class.role_human_readable role }
|
|
56
64
|
end
|
|
57
65
|
|
|
58
66
|
|
|
@@ -89,39 +97,42 @@ module RubyRoles
|
|
|
89
97
|
#
|
|
90
98
|
#
|
|
91
99
|
|
|
100
|
+
def get_roles_field
|
|
101
|
+
self.class.get_roles_field
|
|
102
|
+
end
|
|
92
103
|
# Set the valid set of roles for a given user
|
|
93
104
|
def roles=(*roles)
|
|
94
|
-
self.
|
|
105
|
+
self.send("#{ self.get_roles_field }=", self.class.roles_syms_to_bits(*roles) )
|
|
95
106
|
self.roles
|
|
96
107
|
end
|
|
97
108
|
|
|
98
109
|
# Return the list of roles this user has
|
|
99
110
|
def roles
|
|
100
|
-
self.class.roles_bits_to_syms(
|
|
111
|
+
self.class.roles_bits_to_syms(send(self.get_roles_field))
|
|
101
112
|
end
|
|
102
113
|
|
|
103
114
|
# Returns TRUE if user has this specific role
|
|
104
115
|
def has_role?(role)
|
|
105
|
-
(self.
|
|
116
|
+
(self.send(self.get_roles_field) & self.class.roles_syms_to_bits(role)) !=0
|
|
106
117
|
end
|
|
107
118
|
|
|
108
119
|
# Returns TRUE if user has any of the specified roles
|
|
109
120
|
def has_any_roles?(*roles)
|
|
110
|
-
(self.
|
|
121
|
+
(self.send(self.get_roles_field) & self.class.roles_syms_to_bits(*roles)) !=0
|
|
111
122
|
end
|
|
112
123
|
|
|
113
124
|
# Returns TRUE if user has all the specified roles
|
|
114
125
|
def has_all_roles?(*roles)
|
|
115
126
|
bits = self.class.roles_syms_to_bits(*roles)
|
|
116
|
-
(self.
|
|
127
|
+
(self.send(self.get_roles_field) & bits) == bits
|
|
117
128
|
end
|
|
118
129
|
|
|
119
130
|
def add_roles(*roles)
|
|
120
|
-
self.
|
|
131
|
+
self.send("#{ self.get_roles_field }=", (self.send(self.get_roles_field)||0) | self.class.roles_syms_to_bits(*roles) )
|
|
121
132
|
self.roles
|
|
122
133
|
end
|
|
123
134
|
def delete_roles(*roles)
|
|
124
|
-
self.
|
|
135
|
+
self.send("#{ self.get_roles_field }=", (self.send(self.get_roles_field)||0) & ~self.class.roles_syms_to_bits(*roles) )
|
|
125
136
|
self.roles
|
|
126
137
|
end
|
|
127
138
|
end
|
data/lib/ruby_roles/version.rb
CHANGED
metadata
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
|
2
2
|
name: RubyRoles
|
|
3
3
|
version: !ruby/object:Gem::Version
|
|
4
|
-
version: 0.1.
|
|
4
|
+
version: 0.1.1
|
|
5
5
|
platform: ruby
|
|
6
6
|
authors:
|
|
7
7
|
- Lee Atchison
|
|
@@ -24,6 +24,20 @@ dependencies:
|
|
|
24
24
|
- - ">="
|
|
25
25
|
- !ruby/object:Gem::Version
|
|
26
26
|
version: '6.0'
|
|
27
|
+
- !ruby/object:Gem::Dependency
|
|
28
|
+
name: activerecord
|
|
29
|
+
requirement: !ruby/object:Gem::Requirement
|
|
30
|
+
requirements:
|
|
31
|
+
- - ">="
|
|
32
|
+
- !ruby/object:Gem::Version
|
|
33
|
+
version: '6.0'
|
|
34
|
+
type: :runtime
|
|
35
|
+
prerelease: false
|
|
36
|
+
version_requirements: !ruby/object:Gem::Requirement
|
|
37
|
+
requirements:
|
|
38
|
+
- - ">="
|
|
39
|
+
- !ruby/object:Gem::Version
|
|
40
|
+
version: '6.0'
|
|
27
41
|
description: Assign roles to users, accounts, user_accounts, and other models in a
|
|
28
42
|
Ruby on Rails application.
|
|
29
43
|
email:
|
|
@@ -42,6 +56,8 @@ files:
|
|
|
42
56
|
- README.md
|
|
43
57
|
- Rakefile
|
|
44
58
|
- lib/RubyRoles.rb
|
|
59
|
+
- lib/ruby_roles/acts_as_ruby_roles/class_methods.rb
|
|
60
|
+
- lib/ruby_roles/acts_as_ruby_roles/instance_methods.rb
|
|
45
61
|
- lib/ruby_roles/concerns/roles_mgmt.rb
|
|
46
62
|
- lib/ruby_roles/version.rb
|
|
47
63
|
- sig/RubyRoles.rbs
|