mongoid_roles 0.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.
data/.gitignore ADDED
@@ -0,0 +1,4 @@
1
+ *.gem
2
+ .bundle
3
+ Gemfile.lock
4
+ pkg/*
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source "http://rubygems.org"
2
+
3
+ # Specify your gem's dependencies in mongoid_roles.gemspec
4
+ gemspec
data/README.md ADDED
@@ -0,0 +1,4 @@
1
+ Mongoid Roles
2
+ =============
3
+
4
+ Roles system for mongoid with interface based on [acl9](https://github.com/be9/acl9).
data/Rakefile ADDED
@@ -0,0 +1,2 @@
1
+ require 'bundler'
2
+ Bundler::GemHelper.install_tasks
@@ -0,0 +1,4 @@
1
+ module Mongoid
2
+ module Roles
3
+ end
4
+ end
@@ -0,0 +1,37 @@
1
+ module Mongoid
2
+ module Roles
3
+ module Object
4
+ extend ActiveSupport::Concern
5
+
6
+ # object.accepts_role?(role_name, subject). An alias for subject.has_role?(role_name, object).
7
+ def accepts_role?(role_name, subject)
8
+ subject.has_role?(role_name, self)
9
+ end
10
+
11
+ # object.accepts_role!(role_name, subject). An alias for subject.has_role!(role_name, object).
12
+ def accepts_role!(role_name, subject)
13
+ subject.has_role!(role_name, self)
14
+ end
15
+
16
+ # object.accepts_no_role!(role_name, subject). An alias for subject.has_no_role!(role_name, object).
17
+ def accepts_no_role!(role_name, subject)
18
+ subject.has_no_role!(role_name, self)
19
+ end
20
+
21
+ # object.accepts_roles_by?(subject). An alias for subject.has_roles_for?(object).
22
+ def accepts_roles_by?(subject)
23
+ subject.has_roles_for?(self)
24
+ end
25
+
26
+ # object.accepts_role_by?(subject). Same as accepts_roles_by?.
27
+ def accepts_role_by?(subject)
28
+ accepts_roles_by?(subject)
29
+ end
30
+
31
+ # object.accepts_roles_by(subject). An alias for subject.roles_for(object).
32
+ def accepts_roles_by(subject)
33
+ subject.roles_for(self)
34
+ end
35
+ end
36
+ end
37
+ end
@@ -0,0 +1,72 @@
1
+ module Mongoid
2
+ module Roles
3
+ class Role
4
+ include Mongoid::Document
5
+
6
+ embedded_in :auth_subject
7
+
8
+ field :role, :type => Symbol
9
+ field :auth_object_type, :type => String
10
+ field :auth_object_id, :type => String
11
+
12
+ scope :find_role_with_object, lambda{|role, object| where(:role => role, :auth_object_type => object.class.name, :auth_object_id => object.id)}
13
+ scope :find_object, lambda{|object| where(:auth_object_type => object.class.name, :auth_object_id => object.id)}
14
+ # validates_uniqueness_of :name
15
+
16
+ class << self
17
+
18
+ # subject.has_role?(role, object = nil). Returns true of false (has or has not).
19
+ def has_role? (role, object)
20
+ find_role_with_object(role,object).count == 1
21
+ end
22
+
23
+ # subject.has_role!(role, object = nil). Assigns a role for the object to the subject.
24
+ # Does nothing is subject already has such a role.
25
+ # IMPELEMENTED IN MongoidRoles::Subject
26
+
27
+ # subject.has_no_role!(role, object = nil). Unassigns a role from the subject.
28
+ def has_no_role! (role, object)
29
+ find_role_with_object(role,object).destroy_all
30
+ end
31
+
32
+ # subject.has_roles_for?(object). Does the subject has any roles for object? (true of false)
33
+ def has_roles_for? (object)
34
+ find_object(object).count > 0
35
+ end
36
+
37
+ # subject.has_role_for?(object). Same as has_roles_for?.
38
+ def has_role_for? (object)
39
+ has_roles_for?(object)
40
+ end
41
+
42
+ # subject.roles_for(object). Returns an array of Role instances, corresponding to subject ’s roles on
43
+ # object. E.g. subject.roles_for(object).map(&:name).sort will give you role names in alphabetical order.
44
+ def roles_for (object)
45
+ find_object(object)
46
+ end
47
+
48
+ # subject.has_no_roles_for!(object). Unassign any subject ’s roles for a given object.
49
+ def has_no_roles_for! (object)
50
+ find_object(object).destroy_all
51
+ end
52
+
53
+ # subject.has_no_roles!. Unassign all roles from subject.
54
+ def has_no_roles!
55
+ criteria.destroy_all
56
+ end
57
+
58
+ end
59
+
60
+ def auth_object
61
+ @auth_object ||= if auth_object_type && auth_object_id
62
+ auth_object_type.constantize.find(auth_object_id)
63
+ end
64
+ end
65
+
66
+ def auth_object=(auth_object)
67
+ self.auth_object_type = auth_object.class.name
68
+ self.auth_object_id = auth_object.id
69
+ end
70
+ end
71
+ end
72
+ end
@@ -0,0 +1,50 @@
1
+ module Mongoid
2
+ module Roles
3
+ module Subject
4
+ extend ActiveSupport::Concern
5
+
6
+ included do
7
+ embeds_many :roles, :class_name => 'Mongoid::Roles::Role'
8
+
9
+ # subject.has_role?(role, object = nil). Returns true of false (has or has not).
10
+ delegate :has_role?, :to => :roles
11
+
12
+ # Unassigns a role from the subject.
13
+ delegate :has_no_role!, :to => :roles
14
+
15
+ # Does the subject has any roles for object? (true of false)
16
+ delegate :has_roles_for?, :to => :roles
17
+
18
+ # Same as has_roles_for?.
19
+ delegate :has_role_for?, :to => :roles
20
+
21
+ # Returns an array of Role instances, corresponding to subject ’s roles on
22
+ # object. E.g. subject.roles_for(object).map(&:name).sort will give you role names in alphabetical order.
23
+ delegate :roles_for, :to => :roles
24
+
25
+ # Unassign any subject ’s roles for a given object.
26
+ delegate :has_no_roles_for!, :to => :roles
27
+
28
+ # Unassign all roles from subject.
29
+ delegate :has_no_roles!, :to => :roles
30
+ end
31
+
32
+ module ClassMethods
33
+
34
+ # find all subjects which has given role for given object
35
+ def with_role role, object
36
+ c = where('roles.auth_object_type' => object.class.name, 'roles.role' => role)
37
+ c.selector['roles.auth_object_id'] = object.id.to_s # FIX, bo chcemy string a nie objectid...
38
+ return c
39
+ end
40
+
41
+ end
42
+
43
+ # Assigns a role for the object to the subject.
44
+ # Does nothing is subject already has such a role.
45
+ def has_role! (role, object = nil)
46
+ roles.find_or_create_by(:role => role, :auth_object => object)
47
+ end
48
+ end
49
+ end
50
+ end
@@ -0,0 +1,3 @@
1
+ module MongoidRoles
2
+ VERSION = "0.0.1"
3
+ end
@@ -0,0 +1,4 @@
1
+ require 'mongoid/roles'
2
+ require 'mongoid/roles/object'
3
+ require 'mongoid/roles/role'
4
+ require 'mongoid/roles/subject'
@@ -0,0 +1,22 @@
1
+ # -*- encoding: utf-8 -*-
2
+ $:.push File.expand_path("../lib", __FILE__)
3
+ require "mongoid/roles/version"
4
+
5
+ Gem::Specification.new do |s|
6
+ s.name = "mongoid_roles"
7
+ s.version = MongoidRoles::VERSION
8
+ s.platform = Gem::Platform::RUBY
9
+ s.authors = ["Michal Wychowaniec"]
10
+ s.email = ["michal.wychowaniec@gmail.com"]
11
+ s.homepage = ""
12
+ s.summary = %q{Mongoid Roles}
13
+ s.description = %q{Mongoid Roles}
14
+
15
+ s.rubyforge_project = "mongoid_roles"
16
+
17
+ s.files = `git ls-files`.split("\n")
18
+ s.test_files = `git ls-files -- {test,spec,features}/*`.split("\n")
19
+ s.executables = `git ls-files -- bin/*`.split("\n").map{ |f| File.basename(f) }
20
+ s.require_paths = ["lib"]
21
+
22
+ end
metadata ADDED
@@ -0,0 +1,75 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: mongoid_roles
3
+ version: !ruby/object:Gem::Version
4
+ prerelease: false
5
+ segments:
6
+ - 0
7
+ - 0
8
+ - 1
9
+ version: 0.0.1
10
+ platform: ruby
11
+ authors:
12
+ - Michal Wychowaniec
13
+ autorequire:
14
+ bindir: bin
15
+ cert_chain: []
16
+
17
+ date: 2011-02-27 00:00:00 +01:00
18
+ default_executable:
19
+ dependencies: []
20
+
21
+ description: Mongoid Roles
22
+ email:
23
+ - michal.wychowaniec@gmail.com
24
+ executables: []
25
+
26
+ extensions: []
27
+
28
+ extra_rdoc_files: []
29
+
30
+ files:
31
+ - .gitignore
32
+ - Gemfile
33
+ - README.md
34
+ - Rakefile
35
+ - lib/mongoid/roles.rb
36
+ - lib/mongoid/roles/object.rb
37
+ - lib/mongoid/roles/role.rb
38
+ - lib/mongoid/roles/subject.rb
39
+ - lib/mongoid/roles/version.rb
40
+ - lib/mongoid_roles.rb
41
+ - mongoid_roles.gemspec
42
+ has_rdoc: true
43
+ homepage: ""
44
+ licenses: []
45
+
46
+ post_install_message:
47
+ rdoc_options: []
48
+
49
+ require_paths:
50
+ - lib
51
+ required_ruby_version: !ruby/object:Gem::Requirement
52
+ none: false
53
+ requirements:
54
+ - - ">="
55
+ - !ruby/object:Gem::Version
56
+ segments:
57
+ - 0
58
+ version: "0"
59
+ required_rubygems_version: !ruby/object:Gem::Requirement
60
+ none: false
61
+ requirements:
62
+ - - ">="
63
+ - !ruby/object:Gem::Version
64
+ segments:
65
+ - 0
66
+ version: "0"
67
+ requirements: []
68
+
69
+ rubyforge_project: mongoid_roles
70
+ rubygems_version: 1.3.7
71
+ signing_key:
72
+ specification_version: 3
73
+ summary: Mongoid Roles
74
+ test_files: []
75
+