roles_mongo_mapper 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.
data/.document ADDED
@@ -0,0 +1,5 @@
1
+ README.rdoc
2
+ lib/**/*.rb
3
+ bin/*
4
+ features/**/*.feature
5
+ LICENSE
data/.gitignore ADDED
@@ -0,0 +1,21 @@
1
+ ## MAC OS
2
+ .DS_Store
3
+
4
+ ## TEXTMATE
5
+ *.tmproj
6
+ tmtags
7
+
8
+ ## EMACS
9
+ *~
10
+ \#*
11
+ .\#*
12
+
13
+ ## VIM
14
+ *.swp
15
+
16
+ ## PROJECT::GENERAL
17
+ coverage
18
+ rdoc
19
+ pkg
20
+
21
+ ## PROJECT::SPECIFIC
data/.rspec ADDED
@@ -0,0 +1 @@
1
+ --format nested --color
data/LICENSE ADDED
@@ -0,0 +1,20 @@
1
+ Copyright (c) 2009 Kristian Mandrup
2
+
3
+ Permission is hereby granted, free of charge, to any person obtaining
4
+ a copy of this software and associated documentation files (the
5
+ "Software"), to deal in the Software without restriction, including
6
+ without limitation the rights to use, copy, modify, merge, publish,
7
+ distribute, sublicense, and/or sell copies of the Software, and to
8
+ permit persons to whom the Software is furnished to do so, subject to
9
+ the following conditions:
10
+
11
+ The above copyright notice and this permission notice shall be
12
+ included in all copies or substantial portions of the Software.
13
+
14
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
15
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
16
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
17
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
18
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
19
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
20
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/README.markdown ADDED
@@ -0,0 +1,62 @@
1
+ # Roles for Mongo Mapper
2
+
3
+ A *Mongo Mapper* implementation of [roles generic](http://github.com/kristianmandrup/roles_generic)
4
+
5
+ ## Intro
6
+
7
+ Implements the [roles generic](http://github.com/kristianmandrup/roles_generic) Roles API
8
+ It also implements the following Role strategies:
9
+
10
+ * admin_flag
11
+ * many_roles
12
+ * one_role
13
+ * roles_mask
14
+ * role_string
15
+ * role_strings
16
+
17
+ ## Install
18
+
19
+ <code>gem install roles_mongo_mapper</code>
20
+
21
+ ## Rails generator
22
+
23
+
24
+ The library comes with a Rails 3 generator that lets you populate a user model with a role strategy of your choice.
25
+
26
+ The following role strategies are included by default. Add your own by adding extra files inside the strategy folder, one file for each role strategy is recommended.
27
+
28
+ * admin_flag
29
+ * many_roles
30
+ * one_role
31
+ * roles_mask
32
+ * role_string
33
+ * role_strings
34
+
35
+ *Roles generator*
36
+
37
+ Apply :admin_flag Role strategy to User model using default roles :admin and :guest (default)
38
+
39
+ <code>$ rails g mongo_mapper:roles User --strategy admin_flag</code>
40
+
41
+ Apply :admin_flag Role strategy to User model using default roles and extra role :author
42
+
43
+ <code>$ rails g mongo_mapper:roles_migration User --strategy admin_flag --roles author</code>
44
+
45
+ Apply :one_role Role strategy to User model without default roles, only with roles :user, :special and :editor
46
+
47
+ <code>$ rails g mongo_mapper:roles_migration User --strategy one_role --roles user special editor --no-default-roles</code>
48
+
49
+
50
+ ## Note on Patches/Pull Requests
51
+
52
+ * Fork the project.
53
+ * Make your feature addition or bug fix.
54
+ * Add tests for it. This is important so I don't break it in a
55
+ future version unintentionally.
56
+ * Commit, do not mess with rakefile, version, or history.
57
+ (if you want to have your own version, that is fine but bump version in a commit by itself I can ignore when I pull)
58
+ * Send me a pull request. Bonus points for topic branches.
59
+
60
+ ## Copyright
61
+
62
+ Copyright (c) 2010 Kristian Mandrup. See LICENSE for details.
data/Rakefile ADDED
@@ -0,0 +1,26 @@
1
+ begin
2
+ require 'jeweler'
3
+ Jeweler::Tasks.new do |gem|
4
+ gem.name = "roles_mongo_mapper"
5
+ gem.summary = %Q{Implementation of Roles generic API for MongoMapper}
6
+ gem.description = %Q{Makes it easy to set a role strategy on your User model in MongoMapper}
7
+ gem.email = "kmandrup@gmail.com"
8
+ gem.homepage = "http://github.com/kristianmandrup/roles_mongo_mapper"
9
+ gem.authors = ["Kristian Mandrup"]
10
+
11
+ gem.add_development_dependency "rspec", "~> 2.0.0.beta.22"
12
+ gem.add_development_dependency 'generator-spec', "~> 0.6.4"
13
+
14
+ gem.add_dependency "mongo_mapper", '~> 0.8.4'
15
+ gem.add_dependency "activesupport", '~> 3.0.0'
16
+ gem.add_dependency "require_all", '~> 1.1.0'
17
+ gem.add_dependency "sugar-high", '~> 0.2.10'
18
+ gem.add_dependency "roles_generic", '~> 0.2.6'
19
+ gem.add_dependency 'logging_assist', '~> 0.1.3'
20
+
21
+ end
22
+ Jeweler::GemcutterTasks.new
23
+ rescue LoadError
24
+ puts "Jeweler (or a dependency) not available. Install it with: gem install jeweler"
25
+ end
26
+
data/VERSION ADDED
@@ -0,0 +1 @@
1
+ 0.1.1
@@ -0,0 +1,69 @@
1
+ require 'rails3_artifactor'
2
+ require 'logging_assist'
3
+
4
+ module MongoMapper
5
+ module Generators
6
+ class RolesGenerator < Rails::Generators::NamedBase
7
+ desc "Add role strategy to a model"
8
+
9
+ class_option :strategy, :type => :string, :aliases => "-s", :default => 'role_string',
10
+ :desc => "Role strategy to use (admin_flag, role_string, roles_string, role_strings, one_role, many_roles, roles_mask)"
11
+
12
+
13
+ class_option :roles, :type => :array, :aliases => "-r", :default => [], :desc => "Valid roles"
14
+
15
+ def apply_role_strategy
16
+ logger.add_logfile
17
+ logger.debug "apply_role_strategy for : #{strategy} in model #{name}"
18
+ insert_into_model name do
19
+ insertion_text
20
+ end
21
+ end
22
+
23
+ protected
24
+
25
+ extend Rails3::Assist::UseMacro
26
+ use_orm :mongo_mapper
27
+
28
+ include Rails3::Assist::BasicLogger
29
+
30
+ def orm
31
+ :mongo_mapper
32
+ end
33
+
34
+ def default_roles
35
+ [:admin, :guest]
36
+ end
37
+
38
+ def roles_to_add
39
+ @roles_to_add ||= default_roles.concat(options[:roles]).to_symbols.uniq
40
+ end
41
+
42
+ def roles
43
+ roles_to_add.map{|r| ":#{r}" }
44
+ end
45
+
46
+ def role_strategy_statement
47
+ "strategy :#{strategy}, :default\n#{role_class_stmt}"
48
+ end
49
+
50
+ def role_class_stmt
51
+ " role_class :role" if [:one_role, :many_roles].include? (strategy.to_sym)
52
+ end
53
+
54
+ def roles_statement
55
+ roles ? "valid_roles_are #{roles.join(', ')}" : ''
56
+ end
57
+
58
+ def insertion_text
59
+ %Q{include Roles::#{orm.to_s.camelize}
60
+ #{role_strategy_statement}
61
+ #{roles_statement}}
62
+ end
63
+
64
+ def strategy
65
+ options[:strategy]
66
+ end
67
+ end
68
+ end
69
+ end
@@ -0,0 +1,8 @@
1
+ require 'roles_generic'
2
+ require 'require_all'
3
+ require 'set'
4
+ require 'sugar-high/array'
5
+ require 'mongo_mapper'
6
+ require 'roles_mongo_mapper/namespaces'
7
+ require 'roles_mongo_mapper/base'
8
+ require 'roles_mongo_mapper/strategy'
@@ -0,0 +1,34 @@
1
+ module Roles::Base
2
+ def valid_roles_are(*role_list)
3
+ strategy_class.valid_roles = role_list.to_symbols
4
+ end
5
+ end
6
+
7
+ module Roles::MongoMapper
8
+ def self.included(base)
9
+ base.extend Roles::Base
10
+ base.extend ClassMethods
11
+ base.orm_name = :mongo_mapper
12
+ end
13
+
14
+ module ClassMethods
15
+
16
+ MAP = {
17
+ :admin_flag => "key :admin_flag, Boolean",
18
+ # :many_roles => "references_many :many_roles, :stored_as => :array, :class_name => 'Role', :default => []",
19
+ # :one_role => "references_one :one_role, :class_name => 'Role'",
20
+ :roles_mask => "key :roles_mask, Integer, :default => 1",
21
+ :role_string => "key :role_string, String",
22
+ :role_strings => "key :role_strings, Array",
23
+ :roles_string => "key :roles_string, String"
24
+ }
25
+
26
+ def strategy name, options = nil
27
+ if options == :default && MAP[name]
28
+ instance_eval MAP[name]
29
+ end
30
+
31
+ role_strategy name, options
32
+ end
33
+ end
34
+ end
@@ -0,0 +1,12 @@
1
+ require 'sugar-high/module'
2
+
3
+ module Roles
4
+ modules :mongo_mapper do
5
+ nested_modules :user, :role
6
+ end
7
+ modules :base, :strategy
8
+ end
9
+
10
+ module RoleStrategy
11
+ modules :mongo_mapper
12
+ end
@@ -0,0 +1,31 @@
1
+ module Roles::Base
2
+ def valid_roles_are(*role_list)
3
+ strategy_class.valid_roles = role_list.to_symbols
4
+ if role_class_name
5
+ role_list.each do |name|
6
+ role_class_name.create(:name => name.to_s).save
7
+ end
8
+ end
9
+ end
10
+ end
11
+
12
+ class Role
13
+ include MongoMapper::Document
14
+ key :name, String
15
+
16
+ scope :by_name, lambda { |name| where(:name => name.to_s) }
17
+ scope :by_names, lambda { |*names| where(:name => names.to_strings) }
18
+
19
+
20
+ class << self
21
+ def find_roles(*role_names)
22
+ # return all(:name => role_names.to_strings)
23
+ by_names(*role_names).all
24
+ end
25
+
26
+ def find_role role_name
27
+ raise ArgumentError, "#find_role takes a single role name as argument, not: #{role_name.inspect}" if !role_name.kind_of_label?
28
+ by_name(role_name).first
29
+ end
30
+ end
31
+ end
@@ -0,0 +1,15 @@
1
+ require 'sugar-high/file'
2
+ require 'sugar-high/array'
3
+
4
+ module Roles::Strategy
5
+ class << self
6
+ def role_dir
7
+ File.dirname(__FILE__)
8
+ end
9
+
10
+ def gem_name
11
+ :roles_mongo_mapper
12
+ end
13
+ end
14
+ end
15
+
@@ -0,0 +1,69 @@
1
+ module RoleStrategy::MongoMapper
2
+ module ManyRoles
3
+ def self.default_role_attribute
4
+ :many_roles
5
+ end
6
+
7
+ def self.included base
8
+ base.extend Roles::Generic::Role::ClassMethods
9
+ base.extend ClassMethods
10
+ base.key :many_roles_ids, Array, :typecast => 'ObjectId'
11
+ base.many :many_roles, class_name: 'Role', :in => :many_roles_ids
12
+ base.ensure_index :many_role_ids
13
+ end
14
+
15
+ module ClassMethods
16
+ def role_attribute
17
+ strategy_class.roles_attribute_name
18
+ end
19
+
20
+ def role_id_attribute
21
+ "#{role_attribute}_ids".to_sym
22
+ end
23
+
24
+ def in_role(role_name)
25
+ role = Role.find_role(role_name)
26
+ res = all(role_id_attribute => role.id)
27
+ end
28
+
29
+ def in_roles(*role_names)
30
+ role_ids = Role.find_roles(role_names).map{|role| role.id}
31
+ all(role_id_attribute.in => role_ids)
32
+ end
33
+ end
34
+
35
+ module Implementation
36
+ def role_id_attribute
37
+ "#{role_attribute}_ids".to_sym
38
+ end
39
+
40
+ # assign roles
41
+ def roles=(*role_names)
42
+ raise "Role class #{role_class} does not have a #find_role(role) method" if !role_class.respond_to? :find_role
43
+ role_relations = role_class.find_roles(*role_names)
44
+ self.send("#{role_attribute}=", role_relations)
45
+ save
46
+ end
47
+
48
+ def add_roles(*role_names)
49
+ raise "Role class #{role_class} does not have a #find_role(role) method" if !role_class.respond_to? :find_role
50
+ role_relations = role_class.find_roles(*role_names)
51
+ self.send(role_attribute) << role_relations
52
+ save
53
+ end
54
+
55
+ # query assigned roles
56
+ def roles
57
+ self.send(role_attribute)
58
+ end
59
+
60
+ def roles_list
61
+ [roles].flatten.map{|r| r.name }.compact.to_symbols
62
+ end
63
+ end
64
+
65
+ extend Roles::Generic::User::Configuration
66
+ configure :type => :role_class
67
+ end
68
+ end
69
+
@@ -0,0 +1,59 @@
1
+ module RoleStrategy::MongoMapper
2
+ module RoleStrings
3
+ def self.default_role_attribute
4
+ :role_strings
5
+ end
6
+
7
+ def self.included base
8
+ base.extend ClassMethods
9
+ end
10
+
11
+ module ClassMethods
12
+ def role_attribute
13
+ strategy_class.roles_attribute_name.to_sym
14
+ end
15
+
16
+ def in_role(role_name)
17
+ in_roles(role_name)
18
+ end
19
+
20
+ def in_roles(*role_names)
21
+ begin
22
+ where(role_attribute.in => role_names.to_strings)
23
+ rescue
24
+ return []
25
+ end
26
+ end
27
+ end
28
+
29
+ module Implementation
30
+ # assign roles
31
+ def roles=(*new_roles)
32
+ new_roles = new_roles.flatten.map{|r| r.to_s if valid_role?(r)}.compact
33
+ if new_roles && new_roles.not.empty?
34
+ self.send("#{role_attribute}=", new_roles.compact.uniq)
35
+ end
36
+ end
37
+ alias_method :role=, :roles=
38
+
39
+ def add_roles(*roles_to_add)
40
+ roles_to_add = roles_to_add.flatten.map{|r| r.to_s if valid_role?(r)}.compact
41
+ if new_roles && new_roles.not.empty?
42
+ self.send(role_attribute) << new_roles.compact.uniq
43
+ end
44
+ end
45
+
46
+ # query assigned roles
47
+ def roles
48
+ self.send(role_attribute).map{|r| r.to_sym}
49
+ end
50
+
51
+ def roles_list
52
+ [roles].flatten
53
+ end
54
+ end
55
+
56
+ extend Roles::Generic::User::Configuration
57
+ configure
58
+ end
59
+ end