unix_like_permissions 0.4.13
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/.gitignore +11 -0
- data/.gitlab-ci.yml +10 -0
- data/.rspec +3 -0
- data/CODE_OF_CONDUCT.md +84 -0
- data/LICENSE.txt +21 -0
- data/README.md +199 -0
- data/Rakefile +8 -0
- data/lib/unix_like_permissions/permission_series.rb +124 -0
- data/lib/unix_like_permissions/types/permission_series.rb +25 -0
- data/lib/unix_like_permissions/version.rb +5 -0
- data/lib/unix_like_permissions.rb +31 -0
- data/spec/permission_series_spec.rb +62 -0
- data/spec/spec_helper.rb +15 -0
- data/unix_like_permissions.gemspec +30 -0
- metadata +77 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA256:
|
3
|
+
metadata.gz: c8078deb84ac8306f4eeec8f52a9b1bc470578ee894cd051cf0ae123b920b360
|
4
|
+
data.tar.gz: 6392731df25b5cb8f014a117ec4de89d26bd90e274526faca801dcfe6b08b5ae
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: f6ee6be113f3e935be36b65251aab44776059390e0e1befe05c68f3e030601251b91cdbb23c4eeb1fefcfe23e9d92da2aa10eaa1db0e51486408382c3dc429fd
|
7
|
+
data.tar.gz: 8eda9ea4bc7fe2da0fff16926fb9f70b6779e7538b52602bb286b61658c087194f20ab621994138b0a322ebbfac7542d14607fc96107fc99a22fb8072cb69a49
|
data/.gitignore
ADDED
data/.gitlab-ci.yml
ADDED
data/.rspec
ADDED
data/CODE_OF_CONDUCT.md
ADDED
@@ -0,0 +1,84 @@
|
|
1
|
+
# Contributor Covenant Code of Conduct
|
2
|
+
|
3
|
+
## Our Pledge
|
4
|
+
|
5
|
+
We as members, contributors, and leaders pledge to make participation in our community a harassment-free experience for everyone, regardless of age, body size, visible or invisible disability, ethnicity, sex characteristics, gender identity and expression, level of experience, education, socio-economic status, nationality, personal appearance, race, religion, or sexual identity and orientation.
|
6
|
+
|
7
|
+
We pledge to act and interact in ways that contribute to an open, welcoming, diverse, inclusive, and healthy community.
|
8
|
+
|
9
|
+
## Our Standards
|
10
|
+
|
11
|
+
Examples of behavior that contributes to a positive environment for our community include:
|
12
|
+
|
13
|
+
* Demonstrating empathy and kindness toward other people
|
14
|
+
* Being respectful of differing opinions, viewpoints, and experiences
|
15
|
+
* Giving and gracefully accepting constructive feedback
|
16
|
+
* Accepting responsibility and apologizing to those affected by our mistakes, and learning from the experience
|
17
|
+
* Focusing on what is best not just for us as individuals, but for the overall community
|
18
|
+
|
19
|
+
Examples of unacceptable behavior include:
|
20
|
+
|
21
|
+
* The use of sexualized language or imagery, and sexual attention or
|
22
|
+
advances of any kind
|
23
|
+
* Trolling, insulting or derogatory comments, and personal or political attacks
|
24
|
+
* Public or private harassment
|
25
|
+
* Publishing others' private information, such as a physical or email
|
26
|
+
address, without their explicit permission
|
27
|
+
* Other conduct which could reasonably be considered inappropriate in a
|
28
|
+
professional setting
|
29
|
+
|
30
|
+
## Enforcement Responsibilities
|
31
|
+
|
32
|
+
Community leaders are responsible for clarifying and enforcing our standards of acceptable behavior and will take appropriate and fair corrective action in response to any behavior that they deem inappropriate, threatening, offensive, or harmful.
|
33
|
+
|
34
|
+
Community leaders have the right and responsibility to remove, edit, or reject comments, commits, code, wiki edits, issues, and other contributions that are not aligned to this Code of Conduct, and will communicate reasons for moderation decisions when appropriate.
|
35
|
+
|
36
|
+
## Scope
|
37
|
+
|
38
|
+
This Code of Conduct applies within all community spaces, and also applies when an individual is officially representing the community in public spaces. Examples of representing our community include using an official e-mail address, posting via an official social media account, or acting as an appointed representative at an online or offline event.
|
39
|
+
|
40
|
+
## Enforcement
|
41
|
+
|
42
|
+
Instances of abusive, harassing, or otherwise unacceptable behavior may be reported to the community leaders responsible for enforcement at tee0zed@gmail.com. All complaints will be reviewed and investigated promptly and fairly.
|
43
|
+
|
44
|
+
All community leaders are obligated to respect the privacy and security of the reporter of any incident.
|
45
|
+
|
46
|
+
## Enforcement Guidelines
|
47
|
+
|
48
|
+
Community leaders will follow these Community Impact Guidelines in determining the consequences for any action they deem in violation of this Code of Conduct:
|
49
|
+
|
50
|
+
### 1. Correction
|
51
|
+
|
52
|
+
**Community Impact**: Use of inappropriate language or other behavior deemed unprofessional or unwelcome in the community.
|
53
|
+
|
54
|
+
**Consequence**: A private, written warning from community leaders, providing clarity around the nature of the violation and an explanation of why the behavior was inappropriate. A public apology may be requested.
|
55
|
+
|
56
|
+
### 2. Warning
|
57
|
+
|
58
|
+
**Community Impact**: A violation through a single incident or series of actions.
|
59
|
+
|
60
|
+
**Consequence**: A warning with consequences for continued behavior. No interaction with the people involved, including unsolicited interaction with those enforcing the Code of Conduct, for a specified period of time. This includes avoiding interactions in community spaces as well as external channels like social media. Violating these terms may lead to a temporary or permanent ban.
|
61
|
+
|
62
|
+
### 3. Temporary Ban
|
63
|
+
|
64
|
+
**Community Impact**: A serious violation of community standards, including sustained inappropriate behavior.
|
65
|
+
|
66
|
+
**Consequence**: A temporary ban from any sort of interaction or public communication with the community for a specified period of time. No public or private interaction with the people involved, including unsolicited interaction with those enforcing the Code of Conduct, is allowed during this period. Violating these terms may lead to a permanent ban.
|
67
|
+
|
68
|
+
### 4. Permanent Ban
|
69
|
+
|
70
|
+
**Community Impact**: Demonstrating a pattern of violation of community standards, including sustained inappropriate behavior, harassment of an individual, or aggression toward or disparagement of classes of individuals.
|
71
|
+
|
72
|
+
**Consequence**: A permanent ban from any sort of public interaction within the community.
|
73
|
+
|
74
|
+
## Attribution
|
75
|
+
|
76
|
+
This Code of Conduct is adapted from the [Contributor Covenant][homepage], version 2.0,
|
77
|
+
available at https://www.contributor-covenant.org/version/2/0/code_of_conduct.html.
|
78
|
+
|
79
|
+
Community Impact Guidelines were inspired by [Mozilla's code of conduct enforcement ladder](https://github.com/mozilla/diversity).
|
80
|
+
|
81
|
+
[homepage]: https://www.contributor-covenant.org
|
82
|
+
|
83
|
+
For answers to common questions about this code of conduct, see the FAQ at
|
84
|
+
https://www.contributor-covenant.org/faq. Translations are available at https://www.contributor-covenant.org/translations.
|
data/LICENSE.txt
ADDED
@@ -0,0 +1,21 @@
|
|
1
|
+
The MIT License (MIT)
|
2
|
+
|
3
|
+
Copyright (c) 2023 tee_zed
|
4
|
+
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
7
|
+
in the Software without restriction, including without limitation the rights
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
10
|
+
furnished to do so, subject to the following conditions:
|
11
|
+
|
12
|
+
The above copyright notice and this permission notice shall be included in
|
13
|
+
all copies or substantial portions of the Software.
|
14
|
+
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
21
|
+
THE SOFTWARE.
|
data/README.md
ADDED
@@ -0,0 +1,199 @@
|
|
1
|
+
# UnixLikePermissions
|
2
|
+
|
3
|
+
The `UnixLikePermissions` gem provides a Ruby implementation for managing a Unix-like permissions system. It allows you to manipulate permission flags (such as read, create, update, destroy) using binary operations, offering an ActiveRecord type for easy integration with Rails applications.
|
4
|
+
|
5
|
+
## Installation
|
6
|
+
|
7
|
+
Add this line to your application's Gemfile:
|
8
|
+
|
9
|
+
```ruby
|
10
|
+
gem 'unix_like_permissions'
|
11
|
+
```
|
12
|
+
|
13
|
+
And then execute:
|
14
|
+
|
15
|
+
```shell
|
16
|
+
bundle install
|
17
|
+
```
|
18
|
+
|
19
|
+
Or install it yourself with:
|
20
|
+
|
21
|
+
```shell
|
22
|
+
gem install unix_like_permissions
|
23
|
+
```
|
24
|
+
|
25
|
+
The `UnixLikePermissions` gem utilizes binary operations to manage permissions efficiently. Each permission is associated with a bit within an integer, and the permission set is represented as a binary number. Here's how it works:
|
26
|
+
|
27
|
+
- Each permission is assigned a binary value (`read` is `1`, `create` is `10`, `update` is `100`, `destroy` is `1000` in binary).
|
28
|
+
- Setting a permission turns the corresponding bit to `1` (true), while unsetting it turns the bit to `0` (false).
|
29
|
+
- Binary `OR` (`|`) operations are used to enable permissions.
|
30
|
+
- Binary `AND` (`&`) operations combined with `NOT` (`~`) are used to disable permissions.
|
31
|
+
- Binary `AND` (`&`) operations are used to check if a permission is enabled.
|
32
|
+
|
33
|
+
These operations allow the gem to manage permissions in a performant and memory-efficient way, mirroring the time-tested Unix permission system.
|
34
|
+
|
35
|
+
- **Efficiency**: Managing permissions as bits within a single integer is computationally efficient and fast.
|
36
|
+
- **Flexibility**: Easily define your own set of permissions according to the needs of your application.
|
37
|
+
- **Intuitiveness**: The API is designed to be simple and easy to use, even for developers not familiar with bitwise operations.
|
38
|
+
- **ActiveRecord Integration**: Comes with built-in support for ActiveRecord, making it easy to store permissions in a database.
|
39
|
+
- **Object-Oriented**: Provides a clear object-oriented interface for permission operations.
|
40
|
+
|
41
|
+
## Usage
|
42
|
+
|
43
|
+
After installing the gem, you can use it to define a set of permissions and interact with them in an object-oriented manner.
|
44
|
+
Defining Permissions
|
45
|
+
|
46
|
+
Permissions are represented as a series of bits, with each bit corresponding to a particular permission. You can define and load a custom permissions map or use the default one.
|
47
|
+
Using Default Permissions
|
48
|
+
|
49
|
+
UnixLikePermissions::PermissionSeries object consumes Integer value of a permission code.
|
50
|
+
To create a new PermissionSeries object, you can pass an Integer value to the constructor:
|
51
|
+
|
52
|
+
```ruby
|
53
|
+
permission_series = UnixLikePermissions::PermissionSeries.new(0)
|
54
|
+
```
|
55
|
+
|
56
|
+
0 stands for no permissions set. You can also pass a string to the constructor 0 and then set permissions using the set method:
|
57
|
+
|
58
|
+
```ruby
|
59
|
+
permission_series = UnixLikePermissions::PermissionSeries.new('0')
|
60
|
+
permission_series.set(update: true)
|
61
|
+
permission_series.set(destroy: true)
|
62
|
+
|
63
|
+
permission_series.to_i # => 6
|
64
|
+
permission_series.to_h # => {:update=>true, :destroy=>true, :read=>false, :create=>false}
|
65
|
+
permission_series.update? # => true
|
66
|
+
permission_series.read # => false
|
67
|
+
```
|
68
|
+
|
69
|
+
The gem comes with a default set of permissions:
|
70
|
+
|
71
|
+
read
|
72
|
+
create
|
73
|
+
update
|
74
|
+
destroy
|
75
|
+
|
76
|
+
You can use these permissions as follows:
|
77
|
+
|
78
|
+
```ruby
|
79
|
+
permission_series = UnixLikePermissions::PermissionSeries.new(0)
|
80
|
+
permission_series.set(update: true)
|
81
|
+
permission_series.set(destroy: true)
|
82
|
+
```
|
83
|
+
|
84
|
+
### Loading a Custom Permissions Map
|
85
|
+
|
86
|
+
You can define your own permissions map and load it using the UnixLikePermissions.load_permissions_map! method:
|
87
|
+
|
88
|
+
```ruby
|
89
|
+
custom_permissions = [
|
90
|
+
:show,
|
91
|
+
:impersonate,
|
92
|
+
:delete,
|
93
|
+
:report
|
94
|
+
]
|
95
|
+
|
96
|
+
UnixLikePermissions.load_permissions_map!(custom_permissions)
|
97
|
+
```
|
98
|
+
|
99
|
+
### Checking Permissions
|
100
|
+
|
101
|
+
You can check if a particular permission is set by calling the query methods:
|
102
|
+
|
103
|
+
```ruby
|
104
|
+
permission_series.delete? # => true or false
|
105
|
+
permission_series.impersonate? # => true or false
|
106
|
+
```
|
107
|
+
|
108
|
+
### Serialization for ActiveRecord
|
109
|
+
|
110
|
+
The gem provides a custom type for ActiveRecord, so you can use it directly in your models:
|
111
|
+
|
112
|
+
```ruby
|
113
|
+
# db/migrate/*
|
114
|
+
|
115
|
+
class AddPermissionsToUsers < ActiveRecord::Migration[6.0]
|
116
|
+
def change
|
117
|
+
add_column :users, :permissions, :integer, default: 0
|
118
|
+
end
|
119
|
+
end
|
120
|
+
|
121
|
+
# app/models/user.rb
|
122
|
+
class User < ApplicationRecord
|
123
|
+
attribute :permissions, UnixLikePermissions::Types::PermissionSeries.new
|
124
|
+
end
|
125
|
+
|
126
|
+
# config/initializers/unix_like_permissions.rb
|
127
|
+
require 'unix_like_permissions'
|
128
|
+
require 'unix_like_permissions/types/permission_series'
|
129
|
+
|
130
|
+
ActiveRecord::Type.register(:permission_series_type, UnixLikePermissions::Types::PermissionSeries)
|
131
|
+
|
132
|
+
```
|
133
|
+
|
134
|
+
### API
|
135
|
+
*With default permissions map goes the following API:*
|
136
|
+
|
137
|
+
#### Checking Permissions
|
138
|
+
|
139
|
+
read?: Returns true if the read permission is set.
|
140
|
+
create?: Returns true if the create permission is set.
|
141
|
+
update?: Returns true if the update permission is set.
|
142
|
+
destroy?: Returns true if the destroy permission is set.
|
143
|
+
|
144
|
+
```ruby
|
145
|
+
permissions.update?
|
146
|
+
```
|
147
|
+
|
148
|
+
#### Permission Value
|
149
|
+
|
150
|
+
`#to_i`: Returns the integer value of the permissions.
|
151
|
+
|
152
|
+
```ruby
|
153
|
+
permissions.to_i # => 4
|
154
|
+
```
|
155
|
+
|
156
|
+
#### Serialization
|
157
|
+
|
158
|
+
`#to_h`: Returns a hash with permission names as keys and boolean values indicating whether each permission is set.
|
159
|
+
```ruby
|
160
|
+
permissions.to_h # => { read: false, create: false, update: true, destroy: false }
|
161
|
+
```
|
162
|
+
|
163
|
+
`#to_s`: Returns a string representation of the set permissions.
|
164
|
+
```ruby
|
165
|
+
permissions.to_s # => "update"
|
166
|
+
```
|
167
|
+
|
168
|
+
`#to_a(type = :str)`: Returns an array representation of the permissions. The type parameter can be :str, :int, or :bool.
|
169
|
+
```ruby
|
170
|
+
permissions.to_a(:bool) # => [false, false, true, false]
|
171
|
+
permissions.to_a(:int) # => [0, 0, 1, 0]
|
172
|
+
permissions.to_a(:str) # => ['0', '0', '1', '0]
|
173
|
+
```
|
174
|
+
|
175
|
+
|
176
|
+
#### Miscellaneous
|
177
|
+
|
178
|
+
`#set_all(value)`: Sets all permissions to the boolean value provided (true or false).
|
179
|
+
|
180
|
+
```ruby
|
181
|
+
permissions.set_all(true)
|
182
|
+
|
183
|
+
permissions.to_s # => "read, create, update, destroy"
|
184
|
+
```
|
185
|
+
|
186
|
+
set({ permission: value, ... }): Sets the specified permission to the boolean value provided (true or false).
|
187
|
+
|
188
|
+
```ruby
|
189
|
+
permissions.set(read: true, create: false)
|
190
|
+
permissions.to_s # => "read"
|
191
|
+
```
|
192
|
+
|
193
|
+
## Contributing
|
194
|
+
|
195
|
+
Bug reports and pull requests are welcome on GitHub at [repository URL]. This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the [code of conduct](link to code of conduct).
|
196
|
+
|
197
|
+
## License
|
198
|
+
|
199
|
+
The gem is available as open source under the terms of the MIT License.
|
data/Rakefile
ADDED
@@ -0,0 +1,124 @@
|
|
1
|
+
module UnixLikePermissions
|
2
|
+
class PermissionSeries
|
3
|
+
PermissionSeriesError = Class.new(StandardError)
|
4
|
+
|
5
|
+
PERMISSIONS_MAP = {
|
6
|
+
read: 1,
|
7
|
+
create: 2,
|
8
|
+
update: 4,
|
9
|
+
destroy: 8
|
10
|
+
}.freeze
|
11
|
+
|
12
|
+
TRUE_ID = true.object_id
|
13
|
+
FALSE_ID = false.object_id
|
14
|
+
ALL_TRUE = PERMISSIONS_MAP.values.sum
|
15
|
+
ALL_FALSE = 0
|
16
|
+
|
17
|
+
attr_reader :value
|
18
|
+
|
19
|
+
def initialize(value)
|
20
|
+
@value = value
|
21
|
+
end
|
22
|
+
|
23
|
+
def set(values)
|
24
|
+
raise ArgumentError, "Unknown permission #{values.keys - PERMISSIONS_MAP.keys}" unless values.keys.all? { |permission| PERMISSIONS_MAP.key?(permission) }
|
25
|
+
raise ArgumentError, "Value must be boolean" unless values.values.all? { |value| value.object_id == FALSE_ID || value.object_id == TRUE_ID }
|
26
|
+
|
27
|
+
# binary values for each permission
|
28
|
+
#
|
29
|
+
# |1 | |2 | |4 | |8 |
|
30
|
+
# |0001| |0010 | |0100 | |1000 |
|
31
|
+
# |read| |create| |update| |destroy|
|
32
|
+
#
|
33
|
+
# when we do binary | (or) operation with 0001 and 0100 we get 0101 which is considered as set CREATE to true and is decimal 5 (1 | 4 = 5)
|
34
|
+
# when we do binary & (and) operation with 0101 and 1011(reverted: '~', 0100) we get 0001 which is considered as set CREATE to false and is decimal 1 (5 & ~4 = 1)
|
35
|
+
values.each do |permission, value|
|
36
|
+
if value
|
37
|
+
@value = self.to_i | PERMISSIONS_MAP[permission]
|
38
|
+
else
|
39
|
+
@value = self.to_i & ~PERMISSIONS_MAP[permission]
|
40
|
+
end
|
41
|
+
end
|
42
|
+
|
43
|
+
@stringified_permissions = nil
|
44
|
+
@value
|
45
|
+
end
|
46
|
+
|
47
|
+
def self.define_getters!
|
48
|
+
if defined?(PERMISSIONS_MAP_WAS)
|
49
|
+
PERMISSIONS_MAP_WAS.keys.each do |permission|
|
50
|
+
remove_method "#{permission.to_s.gsub(/[^\w]/, '_')}?"
|
51
|
+
end
|
52
|
+
end
|
53
|
+
|
54
|
+
PERMISSIONS_MAP.keys.each do |permission|
|
55
|
+
define_method "#{permission.to_s.gsub(/[^\w]/, '_')}?" do
|
56
|
+
peek(permission)
|
57
|
+
end
|
58
|
+
end
|
59
|
+
rescue StandardError => e
|
60
|
+
raise PermissionSeriesError, e.message
|
61
|
+
end
|
62
|
+
|
63
|
+
def peek(permission)
|
64
|
+
raise ArgumentError, "Unknown permission #{permission}" unless PERMISSIONS_MAP.key?(permission)
|
65
|
+
|
66
|
+
# binary values for each permission
|
67
|
+
#
|
68
|
+
# |1 | |2 | |4 | |8 |
|
69
|
+
# |0001| |0010 | |0100 | |1000 |
|
70
|
+
# |read| |create| |update| |destroy|
|
71
|
+
#
|
72
|
+
# when we do binary & (and) operation with 0101 and 0010 we get 0000 which is considered as CREATE is false and is decimal 0 (5 & 2 = 0)
|
73
|
+
# when we do binary & (and) operation with 0101 and 0100 we get 0100 which is considered as UPDATE is true and is decimal 4 (5 & 4 = 4)
|
74
|
+
(self.to_i & PERMISSIONS_MAP[permission]) > 0
|
75
|
+
end
|
76
|
+
|
77
|
+
def set_all(value)
|
78
|
+
@value = value ? ALL_TRUE : ALL_FALSE
|
79
|
+
@stringified_permissions = nil
|
80
|
+
@value
|
81
|
+
end
|
82
|
+
|
83
|
+
def to_i
|
84
|
+
@value.to_i
|
85
|
+
end
|
86
|
+
|
87
|
+
def to_a(type = :str)
|
88
|
+
case type
|
89
|
+
when :str
|
90
|
+
stringified_permissions.chars
|
91
|
+
when :int
|
92
|
+
stringified_permissions.chars.map(&:to_i)
|
93
|
+
when :bool
|
94
|
+
stringified_permissions.chars.map { |action| action == '1' }
|
95
|
+
else
|
96
|
+
raise ArgumentError, "Unknown type #{type}"
|
97
|
+
end
|
98
|
+
end
|
99
|
+
|
100
|
+
def to_h(type = :bool)
|
101
|
+
PERMISSIONS_MAP.keys.zip(self.to_a(type)).to_h
|
102
|
+
end
|
103
|
+
|
104
|
+
def to_s
|
105
|
+
result = ''
|
106
|
+
|
107
|
+
stringified_permissions.each_char.with_index do |action, i|
|
108
|
+
if action == '1'
|
109
|
+
result << ' ' unless i.zero?
|
110
|
+
permissions = PERMISSIONS_MAP.keys
|
111
|
+
result << permissions[i].to_s
|
112
|
+
end
|
113
|
+
end
|
114
|
+
|
115
|
+
result
|
116
|
+
end
|
117
|
+
|
118
|
+
private
|
119
|
+
|
120
|
+
def stringified_permissions
|
121
|
+
@stringified_permissions ||= @value.to_s(2).rjust(PERMISSIONS_MAP.size, '0').reverse
|
122
|
+
end
|
123
|
+
end
|
124
|
+
end
|
@@ -0,0 +1,25 @@
|
|
1
|
+
require_relative '../permission_series'
|
2
|
+
|
3
|
+
raise 'You must load ActiveRecord first!' unless defined?(::ActiveRecord)
|
4
|
+
|
5
|
+
module UnixLikePermissions
|
6
|
+
module Types
|
7
|
+
class PermissionSeries < ActiveRecord::Type::Value
|
8
|
+
def type
|
9
|
+
:integer
|
10
|
+
end
|
11
|
+
|
12
|
+
def cast(value)
|
13
|
+
::UnixLikePermissions::PermissionSeries.new(value)
|
14
|
+
end
|
15
|
+
|
16
|
+
def deserialize(value)
|
17
|
+
::UnixLikePermissions::PermissionSeries.new(value)
|
18
|
+
end
|
19
|
+
|
20
|
+
def serialize(value)
|
21
|
+
value.value
|
22
|
+
end
|
23
|
+
end
|
24
|
+
end
|
25
|
+
end
|
@@ -0,0 +1,31 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
require_relative 'unix_like_permissions/version'
|
4
|
+
require_relative 'unix_like_permissions/permission_series'
|
5
|
+
|
6
|
+
module UnixLikePermissions
|
7
|
+
extend self
|
8
|
+
|
9
|
+
PermissionSeries.define_getters!
|
10
|
+
|
11
|
+
def create(permissions)
|
12
|
+
PermissionSeries.new(permissions)
|
13
|
+
end
|
14
|
+
|
15
|
+
def load_permissions_map!(permissions)
|
16
|
+
PermissionSeries.const_set(:PERMISSIONS_MAP_WAS, PermissionSeries::PERMISSIONS_MAP)
|
17
|
+
PermissionSeries.const_set(:PERMISSIONS_MAP, permissions_map(permissions))
|
18
|
+
PermissionSeries::PERMISSIONS_MAP.freeze
|
19
|
+
|
20
|
+
PermissionSeries.define_getters!
|
21
|
+
puts "Permission map loaded: #{PermissionSeries::PERMISSIONS_MAP}"
|
22
|
+
end
|
23
|
+
|
24
|
+
def permissions_map(permissions)
|
25
|
+
raise ArgumentError("Permission map must be array of symbols!") unless permissions.is_a?(Array) && permissions.all? { |permission| permission.is_a?(Symbol) }
|
26
|
+
|
27
|
+
permissions.map.with_index do |permission, index|
|
28
|
+
[permission, 2**index]
|
29
|
+
end.to_h
|
30
|
+
end
|
31
|
+
end
|
@@ -0,0 +1,62 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
require './lib/unix_like_permissions/permission_series'
|
3
|
+
PermissionSeries = UnixLikePermissions::PermissionSeries
|
4
|
+
|
5
|
+
RSpec.describe PermissionSeries do
|
6
|
+
let(:permission_series) { PermissionSeries.new(0) }
|
7
|
+
|
8
|
+
describe '#initialize' do
|
9
|
+
it 'initializes with a value' do
|
10
|
+
expect(permission_series.value).to eq(0)
|
11
|
+
end
|
12
|
+
end
|
13
|
+
|
14
|
+
describe '#set' do
|
15
|
+
it 'sets a permission' do
|
16
|
+
permission_series.set(read: true)
|
17
|
+
expect(permission_series.to_i).to eq(1)
|
18
|
+
end
|
19
|
+
|
20
|
+
it 'raises error for unknown permission' do
|
21
|
+
expect { permission_series.set(unknown_permission: true) }.to raise_error(ArgumentError)
|
22
|
+
end
|
23
|
+
end
|
24
|
+
|
25
|
+
describe '#set_all' do
|
26
|
+
it 'sets all permissions to true' do
|
27
|
+
permission_series.set_all(true)
|
28
|
+
expect(permission_series.to_i).to eq(15)
|
29
|
+
end
|
30
|
+
|
31
|
+
it 'sets all permissions to false' do
|
32
|
+
permission_series.set_all(false)
|
33
|
+
expect(permission_series.to_i).to eq(0)
|
34
|
+
end
|
35
|
+
end
|
36
|
+
|
37
|
+
describe '#to_i' do
|
38
|
+
it 'returns the integer value of the permissions' do
|
39
|
+
expect(permission_series.to_i).to be_a(Integer)
|
40
|
+
end
|
41
|
+
end
|
42
|
+
|
43
|
+
describe '#to_a' do
|
44
|
+
it 'returns permissions as an array of strings when type is :str' do
|
45
|
+
expect(permission_series.to_a(:str)).to eq(["0", "0", "0", "0"])
|
46
|
+
end
|
47
|
+
end
|
48
|
+
|
49
|
+
describe '#to_h' do
|
50
|
+
it 'returns permissions as a hash' do
|
51
|
+
permission_series.set(read: true)
|
52
|
+
expect(permission_series.to_h).to eq({read: true, create: false, update: false, destroy: false})
|
53
|
+
end
|
54
|
+
end
|
55
|
+
|
56
|
+
describe '#to_s' do
|
57
|
+
it 'returns a string representation of the permissions' do
|
58
|
+
permission_series.set(read: true, destroy: true)
|
59
|
+
expect(permission_series.to_s).to eq("read destroy")
|
60
|
+
end
|
61
|
+
end
|
62
|
+
end
|
data/spec/spec_helper.rb
ADDED
@@ -0,0 +1,15 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
require "unix_like_permissions"
|
4
|
+
|
5
|
+
RSpec.configure do |config|
|
6
|
+
# Enable flags like --only-failures and --next-failure
|
7
|
+
config.example_status_persistence_file_path = ".rspec_status"
|
8
|
+
|
9
|
+
# Disable RSpec exposing methods globally on `Module` and `main`
|
10
|
+
config.disable_monkey_patching!
|
11
|
+
|
12
|
+
config.expect_with :rspec do |c|
|
13
|
+
c.syntax = :expect
|
14
|
+
end
|
15
|
+
end
|
@@ -0,0 +1,30 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
require_relative "lib/unix_like_permissions/version"
|
4
|
+
|
5
|
+
Gem::Specification.new do |spec|
|
6
|
+
spec.name = "unix_like_permissions"
|
7
|
+
spec.version = UnixLikePermissions::VERSION
|
8
|
+
spec.authors = ["Your Name"]
|
9
|
+
spec.email = ["your_email@example.com"]
|
10
|
+
|
11
|
+
spec.summary = %q{Unix-like permissions management for Ruby applications}
|
12
|
+
spec.description = %q{This gem provides a robust system for managing Unix-like permissions in Ruby. It allows for setting, querying, and serializing permission sets with an ActiveRecord integration for easy database storage.}
|
13
|
+
spec.homepage = "https://github.com/tee0zed/unix_like_permissions"
|
14
|
+
spec.license = "MIT"
|
15
|
+
spec.required_ruby_version = Gem::Requirement.new(">= 2.6.0")
|
16
|
+
|
17
|
+
spec.metadata = {
|
18
|
+
"allowed_push_host" => "https://rubygems.org", # You can restrict which hosts can push the gem here.
|
19
|
+
"homepage_uri" => spec.homepage,
|
20
|
+
"source_code_uri" => "https://github.com/your_username/unix_like_permissions",
|
21
|
+
"changelog_uri" => "https://github.com/your_username/unix_like_permissions/blob/main/CHANGELOG.md"
|
22
|
+
}
|
23
|
+
|
24
|
+
spec.files = `git ls-files -z`.split("\x0").reject { |f| f.match(%r{\A(bin|test|spec|features|\.git|\.circleci|appveyor|Gemfile)\z}) }
|
25
|
+
spec.bindir = "exe"
|
26
|
+
spec.executables = spec.files.grep(%r{\Aexe/}) { |f| File.basename(f) }
|
27
|
+
spec.require_paths = ["lib"]
|
28
|
+
|
29
|
+
spec.add_development_dependency "rspec"
|
30
|
+
end
|
metadata
ADDED
@@ -0,0 +1,77 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: unix_like_permissions
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.4.13
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Your Name
|
8
|
+
autorequire:
|
9
|
+
bindir: exe
|
10
|
+
cert_chain: []
|
11
|
+
date: 2023-11-07 00:00:00.000000000 Z
|
12
|
+
dependencies:
|
13
|
+
- !ruby/object:Gem::Dependency
|
14
|
+
name: rspec
|
15
|
+
requirement: !ruby/object:Gem::Requirement
|
16
|
+
requirements:
|
17
|
+
- - ">="
|
18
|
+
- !ruby/object:Gem::Version
|
19
|
+
version: '0'
|
20
|
+
type: :development
|
21
|
+
prerelease: false
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - ">="
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: '0'
|
27
|
+
description: This gem provides a robust system for managing Unix-like permissions
|
28
|
+
in Ruby. It allows for setting, querying, and serializing permission sets with an
|
29
|
+
ActiveRecord integration for easy database storage.
|
30
|
+
email:
|
31
|
+
- your_email@example.com
|
32
|
+
executables: []
|
33
|
+
extensions: []
|
34
|
+
extra_rdoc_files: []
|
35
|
+
files:
|
36
|
+
- ".gitignore"
|
37
|
+
- ".gitlab-ci.yml"
|
38
|
+
- ".rspec"
|
39
|
+
- CODE_OF_CONDUCT.md
|
40
|
+
- LICENSE.txt
|
41
|
+
- README.md
|
42
|
+
- Rakefile
|
43
|
+
- lib/unix_like_permissions.rb
|
44
|
+
- lib/unix_like_permissions/permission_series.rb
|
45
|
+
- lib/unix_like_permissions/types/permission_series.rb
|
46
|
+
- lib/unix_like_permissions/version.rb
|
47
|
+
- spec/permission_series_spec.rb
|
48
|
+
- spec/spec_helper.rb
|
49
|
+
- unix_like_permissions.gemspec
|
50
|
+
homepage: https://github.com/tee0zed/unix_like_permissions
|
51
|
+
licenses:
|
52
|
+
- MIT
|
53
|
+
metadata:
|
54
|
+
allowed_push_host: https://rubygems.org
|
55
|
+
homepage_uri: https://github.com/tee0zed/unix_like_permissions
|
56
|
+
source_code_uri: https://github.com/your_username/unix_like_permissions
|
57
|
+
changelog_uri: https://github.com/your_username/unix_like_permissions/blob/main/CHANGELOG.md
|
58
|
+
post_install_message:
|
59
|
+
rdoc_options: []
|
60
|
+
require_paths:
|
61
|
+
- lib
|
62
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
63
|
+
requirements:
|
64
|
+
- - ">="
|
65
|
+
- !ruby/object:Gem::Version
|
66
|
+
version: 2.6.0
|
67
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
68
|
+
requirements:
|
69
|
+
- - ">="
|
70
|
+
- !ruby/object:Gem::Version
|
71
|
+
version: '0'
|
72
|
+
requirements: []
|
73
|
+
rubygems_version: 3.4.17
|
74
|
+
signing_key:
|
75
|
+
specification_version: 4
|
76
|
+
summary: Unix-like permissions management for Ruby applications
|
77
|
+
test_files: []
|