rolistic 0.1.1 → 0.2.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/LICENSE.txt +21 -0
- data/README.md +86 -0
- data/lib/rolistic.rb +33 -0
- data/lib/rolistic/class_methods.rb +65 -0
- data/lib/rolistic/everything.rb +7 -0
- data/lib/rolistic/version.rb +3 -0
- metadata +8 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: f91550f1333bb0466e4a35bf3567cc51c4ef53c7
|
4
|
+
data.tar.gz: 27263d1e23f74b953ddbb807d6d98dd01f39c1c9
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: d6b2a485bf18ed8ee2b5a31072d7df69a7099b14b1986a462d8f8dd2fe97b8106471ef4022b435f6fdd6def7ab22e256d0391da5f77aeab48c7f600be9d9ea98
|
7
|
+
data.tar.gz: 4e7e13bc52bd18ea3bce59ad1de109b31141d3ad26a84b0accf170044e76ef16d655b3fcc20acbf260ef4f5171f1e0dbd4046d68bec1e9662231cd099fc2144c
|
data/LICENSE.txt
ADDED
@@ -0,0 +1,21 @@
|
|
1
|
+
The MIT License (MIT)
|
2
|
+
|
3
|
+
Copyright (c) 2017 Carl Zulauf
|
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,86 @@
|
|
1
|
+
# Rolistic
|
2
|
+
|
3
|
+
A simple DSL for role-based permissions.
|
4
|
+
|
5
|
+
Inspired by tools like [`cancan`](https://github.com/ryanb/cancan) and [`pundit`](https://github.com/elabs/pundit) but intended to be even simpler.
|
6
|
+
|
7
|
+
All permissions are identified by a single ability name. No need for objects or methods to manage policies for every type of domain object. Have a complex edge case? Just create a new ability for it specifically.
|
8
|
+
|
9
|
+
## Installation
|
10
|
+
|
11
|
+
Add this line to your application's Gemfile:
|
12
|
+
|
13
|
+
```ruby
|
14
|
+
gem 'rolistic'
|
15
|
+
```
|
16
|
+
|
17
|
+
And then execute:
|
18
|
+
|
19
|
+
$ bundle
|
20
|
+
|
21
|
+
Or install it yourself as:
|
22
|
+
|
23
|
+
$ gem install rolistic
|
24
|
+
|
25
|
+
## Usage
|
26
|
+
|
27
|
+
Create a class to house your roles and their permissions.
|
28
|
+
|
29
|
+
```ruby
|
30
|
+
class ForumRole
|
31
|
+
include Rolistic
|
32
|
+
# traits allow you to categorize abilities shared by multiple roles
|
33
|
+
trait :moderation, %i(delete_thread edit_thread)
|
34
|
+
|
35
|
+
role :anonymous, %i(vote_in_polls reply_to_thread), default: true
|
36
|
+
|
37
|
+
# roles can inherit abilities from existing roles, plus add their own
|
38
|
+
role :registered, :anonymous, %i(edit_own_thread create_thread)
|
39
|
+
|
40
|
+
role :moderator, :registered, :moderation
|
41
|
+
role :silent_moderator, :moderation
|
42
|
+
|
43
|
+
# special `everything` trait has access to all abilities, even unnamed ones
|
44
|
+
role :developer, :everything
|
45
|
+
end
|
46
|
+
```
|
47
|
+
|
48
|
+
Instances of your role class tell you what they can do.
|
49
|
+
|
50
|
+
```ruby
|
51
|
+
ForumRole.new(:registered).can?(:vote_in_polls) # => true
|
52
|
+
ForumRole.new("moderator").can?(:edit_thread) # => true
|
53
|
+
ForumRole.new(:anonymous).can?(:create_thread) # => false
|
54
|
+
ForumRole.new(:developer).can?(:introduce_bugs) # => true
|
55
|
+
|
56
|
+
# Use the default role
|
57
|
+
ForumRole.new.can?(:reply_to_thread) # => true
|
58
|
+
```
|
59
|
+
|
60
|
+
If you are using `ActiveRecord` your role class can easily be serialized to any string column.
|
61
|
+
|
62
|
+
```ruby
|
63
|
+
class User < ApplicationRecord
|
64
|
+
serialize :role, ForumRole
|
65
|
+
delegate :can?, to: :role
|
66
|
+
end
|
67
|
+
|
68
|
+
User.new(role: "registered").can?(:create_thread) # => true
|
69
|
+
```
|
70
|
+
|
71
|
+
More usage examples can be found in `spec/rolistic_spec.rb`.
|
72
|
+
|
73
|
+
## Development
|
74
|
+
|
75
|
+
After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake spec` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
|
76
|
+
|
77
|
+
To install this gem onto your local machine, run `bundle exec rake install`. To release a new version, update the version number in `version.rb`, and then run `bundle exec rake release`, which will create a git tag for the version, push git commits and tags, and push the `.gem` file to [rubygems.org](https://rubygems.org).
|
78
|
+
|
79
|
+
## Contributing
|
80
|
+
|
81
|
+
Bug reports and pull requests are welcome on GitHub at https://github.com/carlzulauf/rolistic. This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the [Contributor Covenant](http://contributor-covenant.org) code of conduct.
|
82
|
+
|
83
|
+
|
84
|
+
## License
|
85
|
+
|
86
|
+
The gem is available as open source under the terms of the [MIT License](http://opensource.org/licenses/MIT).
|
data/lib/rolistic.rb
ADDED
@@ -0,0 +1,33 @@
|
|
1
|
+
require "rolistic/version"
|
2
|
+
require "rolistic/class_methods"
|
3
|
+
require "rolistic/everything"
|
4
|
+
|
5
|
+
module Rolistic
|
6
|
+
def self.included(klass)
|
7
|
+
klass.class_exec do
|
8
|
+
attr_reader :name, :abilities
|
9
|
+
extend ClassMethods
|
10
|
+
end
|
11
|
+
end
|
12
|
+
|
13
|
+
def initialize(name = self.class.default)
|
14
|
+
@name = name.to_sym if name
|
15
|
+
@abilities = self.class.abilities_for(@name)
|
16
|
+
end
|
17
|
+
|
18
|
+
def to_s
|
19
|
+
name.to_s
|
20
|
+
end
|
21
|
+
|
22
|
+
def can?(ability)
|
23
|
+
abilities.member?(ability)
|
24
|
+
end
|
25
|
+
|
26
|
+
def default?
|
27
|
+
name == self.class.default
|
28
|
+
end
|
29
|
+
|
30
|
+
def inspect
|
31
|
+
"#<#{self.class} :#{to_s}>"
|
32
|
+
end
|
33
|
+
end
|
@@ -0,0 +1,65 @@
|
|
1
|
+
module Rolistic
|
2
|
+
module ClassMethods
|
3
|
+
def load(raw)
|
4
|
+
case raw
|
5
|
+
when self then raw
|
6
|
+
when String, Symbol
|
7
|
+
new(raw) unless raw.blank?
|
8
|
+
else
|
9
|
+
new
|
10
|
+
end
|
11
|
+
end
|
12
|
+
|
13
|
+
def dump(role)
|
14
|
+
case role
|
15
|
+
when String, Symbol then role.to_s
|
16
|
+
when self
|
17
|
+
role.to_s unless role.default?
|
18
|
+
end
|
19
|
+
end
|
20
|
+
|
21
|
+
def abilities_map
|
22
|
+
return @abilities_map if defined?(@abilities_map)
|
23
|
+
@abilities_map = {}
|
24
|
+
end
|
25
|
+
|
26
|
+
def traits_map
|
27
|
+
return @traits_map if defined?(@traits_map)
|
28
|
+
@traits_map = {everything: Everything}
|
29
|
+
end
|
30
|
+
|
31
|
+
def abilities_for(name)
|
32
|
+
abilities_map.fetch(name) { [] }
|
33
|
+
end
|
34
|
+
|
35
|
+
def abilities_for_trait(trait)
|
36
|
+
traits_map.fetch(trait) { abilities_for(name) }
|
37
|
+
end
|
38
|
+
|
39
|
+
def trait(trait, abilities)
|
40
|
+
traits_map[trait] = abilities
|
41
|
+
end
|
42
|
+
alias_method :add_trait, :trait
|
43
|
+
|
44
|
+
def role(name, *traits_and_abilities, **options)
|
45
|
+
abilities = traits_and_abilities.reduce([]) do |m, t_or_a|
|
46
|
+
case t_or_a
|
47
|
+
when Symbol then m + abilities_for_trait(t_or_a)
|
48
|
+
when Array then m + t_or_a
|
49
|
+
else t_or_a
|
50
|
+
end
|
51
|
+
end
|
52
|
+
abilities_map[name] = abilities
|
53
|
+
@default = name if options[:default]
|
54
|
+
end
|
55
|
+
alias_method :add_role, :role
|
56
|
+
|
57
|
+
def default
|
58
|
+
@default if defined?(@default)
|
59
|
+
end
|
60
|
+
|
61
|
+
def available
|
62
|
+
abilities_map.keys
|
63
|
+
end
|
64
|
+
end
|
65
|
+
end
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: rolistic
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.
|
4
|
+
version: 0.2.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Carl Zulauf
|
@@ -58,7 +58,13 @@ email:
|
|
58
58
|
executables: []
|
59
59
|
extensions: []
|
60
60
|
extra_rdoc_files: []
|
61
|
-
files:
|
61
|
+
files:
|
62
|
+
- LICENSE.txt
|
63
|
+
- README.md
|
64
|
+
- lib/rolistic.rb
|
65
|
+
- lib/rolistic/class_methods.rb
|
66
|
+
- lib/rolistic/everything.rb
|
67
|
+
- lib/rolistic/version.rb
|
62
68
|
homepage: https://github.com/carlzulauf/rolistic
|
63
69
|
licenses:
|
64
70
|
- MIT
|