can_play 0.1.6

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 ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 3fbbcdcfbceff8da3ea0f8d1a8484d73bc814894
4
+ data.tar.gz: 04b2983c9fc11afab1c095f471999c42bf2b07df
5
+ SHA512:
6
+ metadata.gz: d1b74feb74f1f6241033a43b4389b3cfcf77dcb83b25b2b4a68677ecc684a7be4de3d9ad7a5ed27c2857e7e85f753f54f977e8ca92386e81ca171fc50a7ff19e
7
+ data.tar.gz: 3d84f0d8b560f66e7272017bed18157dfba01d8d212d1f14c54f6ac4a3f927c84a882b2657f1c93055c2f65b307dc6ff054bcf236e78e57304447054fdbb72bf
data/.gitignore ADDED
@@ -0,0 +1,10 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /Gemfile.lock
4
+ /_yardoc/
5
+ /coverage/
6
+ /doc/
7
+ /pkg/
8
+ /spec/reports/
9
+ /tmp/
10
+ .idea/*
data/Gemfile ADDED
@@ -0,0 +1,6 @@
1
+ source 'https://rubygems.org'
2
+ gem "cancancan"
3
+ gem "consul"
4
+ gem "rolify"
5
+ # Specify your gem's dependencies in can_play.gemspec
6
+ gemspec
data/LICENSE.txt ADDED
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2015 TODO: Write your name
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,94 @@
1
+ can_plan集成了cancancan和consul的功能,使用DSL描述用户对单个类的实例或某个类的操作权限,及可获取的条目的基础的relation对象.
2
+
3
+ ## 安装方式
4
+
5
+ ### 安装cancancan
6
+
7
+ cancancan的使用请参见cancancan主页,在此我们安装后,不需要设置Ability文件,can_play在内部集成了这些设置。
8
+ ### 安装consul
9
+
10
+ consul的使用请参见consul主页,在此我们安装后,不需要设置power文件,也无需在controller中设置current_power,can_play在内部集成了这些设置。
11
+ ### can_play安装
12
+ 在gemfile中加入can_play的github地址来安装。
13
+
14
+ 安装后执行如下命令
15
+
16
+ ```
17
+ rails generate can_play:install
18
+ ```
19
+
20
+ 会在initializer和locales文件夹下生成文件。
21
+ initializer文件夹下的can_play.rb是can_play的基本配置文件。
22
+ locales下的can_play.zh-Cn.yml文件用于描述权限名称。
23
+
24
+ ### DSL文件描述权限
25
+ dsl文件写法如下:
26
+
27
+ #用哪个类用来描写权限,可在intializer下的can_play.rb文件下描写。
28
+ class Resource
29
+ include CanPlay
30
+
31
+ # 所有limit块、collection块和member块中都注入了user这个变量,指向当前登录用户,可直接使用。
32
+
33
+ group Contract do |klass|
34
+
35
+ # 描述某个用户可以查看到哪些合同条目。
36
+ limit do
37
+ if user.is_admin?
38
+ klass.all
39
+ elsif user.role? '供应商'
40
+ klass.where(emall: user.emall)
41
+ elsif user.role? '采购人'
42
+ klass.where(department: user.department)
43
+ else
44
+ klass.none
45
+ end
46
+ end
47
+
48
+ # 描述某个用户可以是否而已查看合同列表、创建合同。
49
+ collection [:list, :create], klass do
50
+ user.is_admin?
51
+ end
52
+
53
+ # 描述某个用户可以是否可以查看、更新某个合同。
54
+ member [:read, :update], klass do |obj|
55
+ if user.is_admin?
56
+ true
57
+ elsif user.role? '供应商'
58
+ obj.emall.is? user.emall
59
+ elsif user.role? '采购人'
60
+ obj.department.is? user.department
61
+ else
62
+ false
63
+ end
64
+ end
65
+
66
+ # 描述某个用户可以是否可以删除、终止某个合同。
67
+ member [:delete, :terminate], klass do |obj|
68
+ user.is_admin?
69
+ end
70
+ end
71
+
72
+ group Project do |klass|
73
+
74
+ limit do
75
+ if user.is_admin?
76
+ klass.all
77
+ else
78
+ klass.none
79
+ end
80
+ end
81
+
82
+ collection [:list, :create], klass do
83
+ user.is_admin?
84
+ end
85
+
86
+ member [:read, :update, :delete, :create_later_documents], klass do |obj|
87
+ user.is_admin?
88
+ end
89
+ end
90
+ end
91
+
92
+ ### 和角色类之间建立关联
93
+
94
+ 此处的DSL相当于在数据库中的resouces表,记录了所有权限。我们需要通过role_resources这样的中间表,建立角色和资源之间的关联。因此在数据库建立中间表role_resources,使用一个resource_name字段来跟DSL中的权限、资源进行关联。我们再前端页面,只需要调用Resource.grouped_resources_with_chinese_desc就可获取到所有DSL文件中描述的所有权限以及中文描述。再在controller和view中创建权限和role的关联即可(往role_resources中间表写条目)。
data/Rakefile ADDED
@@ -0,0 +1 @@
1
+ require "bundler/gem_tasks"
data/bin/console ADDED
@@ -0,0 +1,14 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require "bundler/setup"
4
+ require "can_play"
5
+
6
+ # You can add fixtures and/or initialization code here to make experimenting
7
+ # with your gem easier. You can also use a different console, if you like.
8
+
9
+ # (If you use this, don't forget to add pry to your Gemfile!)
10
+ # require "pry"
11
+ # Pry.start
12
+
13
+ require "irb"
14
+ IRB.start
data/bin/setup ADDED
@@ -0,0 +1,7 @@
1
+ #!/bin/bash
2
+ set -euo pipefail
3
+ IFS=$'\n\t'
4
+
5
+ bundle install
6
+
7
+ # Do any other automated setup that you need to do here
data/can_play.gemspec ADDED
@@ -0,0 +1,27 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'can_play/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "can_play"
8
+ spec.version = CanPlay::VERSION
9
+ spec.authors = ["happyming9527"]
10
+ spec.email = ["happyming9527@gmail.com"]
11
+
12
+ spec.summary = %q{a permission system.}
13
+ spec.description = %q{control user's permissions based on role and resource.}
14
+ spec.homepage = "https://github.com/happyming9527/can_play"
15
+ spec.license = "MIT"
16
+
17
+ spec.files = `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
18
+ spec.bindir = "exe"
19
+ spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
20
+ spec.require_paths = ["lib"]
21
+
22
+ spec.add_development_dependency "bundler", "~> 1.10"
23
+ spec.add_development_dependency "rake", "~> 10.0"
24
+ spec.add_dependency 'cancancan', "~> 1.12"
25
+ spec.add_dependency 'consul', "~> 0.12"
26
+ spec.add_dependency 'ror_hack', "~> 0.1 "
27
+ end
@@ -0,0 +1,17 @@
1
+ ---
2
+ zh-CN:
3
+ can_play:
4
+ class_name:
5
+ can_play: 权限管理模型
6
+
7
+ authority_name:
8
+ common:
9
+ list: 列表查看
10
+ create: 新建
11
+ read: 查看
12
+ update: 修改
13
+ delete: 删除
14
+ plan:
15
+ test: 测试
16
+
17
+
@@ -0,0 +1,32 @@
1
+ class Ability
2
+ include CanCan::Ability
3
+ attr_accessor :user
4
+
5
+ def initialize(user)
6
+ self.user = user||CanPlay::Config.user_class_name.constantize.new
7
+ CanPlay::Config.role_class_name.constantize.all.each do |role|
8
+ next unless user.role?(role.name)
9
+ role.send(CanPlay::Config.role_resources_middle_class_name.underscore.pluralize).each do |role_resource|
10
+ resource = CanPlay::Config.resource_class_name.constantize.find_by_name(role_resource.resource_name)
11
+ next unless resource
12
+ if resource[:type] == 'collection'
13
+ if resource[:behavior]
14
+ block = resource[:behavior]
15
+ can(resource[:verb], resource[:object]) if block.call(user)
16
+ else
17
+ can resource[:verb], resource[:object]
18
+ end
19
+ elsif resource[:type] == 'member'
20
+ if resource[:behavior]
21
+ block = resource[:behavior]
22
+ can resource[:verb], resource[:object] do |object|
23
+ block.call(user, object)
24
+ end
25
+ else
26
+ can resource[:verb], resource[:object]
27
+ end
28
+ end
29
+ end
30
+ end
31
+ end
32
+ end
@@ -0,0 +1,6 @@
1
+ class ActionController::Base
2
+ include Consul::Controller
3
+ current_power do
4
+ Power.new(current_user)
5
+ end
6
+ end
@@ -0,0 +1,9 @@
1
+ class Power
2
+ include Consul::Power
3
+ attr_accessor :user
4
+
5
+ def initialize(user)
6
+ self.user = user
7
+ end
8
+
9
+ end
@@ -0,0 +1,3 @@
1
+ module CanPlay
2
+ VERSION = "0.1.6"
3
+ end
data/lib/can_play.rb ADDED
@@ -0,0 +1,145 @@
1
+ require 'ror_hack'
2
+ require 'consul'
3
+ require 'cancancan'
4
+ require "can_play/version"
5
+ require "can_play/power"
6
+ require "can_play/ability"
7
+ require "can_play/controller"
8
+
9
+ module CanPlay
10
+
11
+ extend ActiveSupport::Concern
12
+
13
+ included do
14
+ @groups = []
15
+ @current_group = nil
16
+ @resources = []
17
+ end
18
+
19
+ module Config
20
+ mattr_accessor :user_class_name, :role_class_name, :role_resources_middle_class_name, :resource_class_name
21
+ @@user_class_name = 'User'
22
+ @@role_class_name = 'Role'
23
+ @@role_resources_middle_class_name = 'RoleResource'
24
+ @@resource_class_name = 'Resource'
25
+
26
+ def self.setup
27
+ yield self
28
+ end
29
+ end
30
+
31
+ module ClassMethods
32
+
33
+ # 为每个 resource 添加一个 group, 方便管理
34
+ def group(opts, &block)
35
+ if opts.is_a?(Hash)
36
+ opts = opts.with_indifferent_access
37
+ group = {name: opts.delete(:name), klass: opts.delete(:klass)}
38
+ elsif opts.is_a?(Module)
39
+ can_play_name = I18n.t("can_play.class_name.#{opts.name.underscore.singularize}", default: '')
40
+ model_name = I18n.t("activerecord.models.#{opts.name.underscore.singularize}", default: '')
41
+ name = (can_play_name.present?||model_name.present?) ? opts.name.underscore.pluralize : opts.try(:table_name)
42
+ group = {name: name, klass: opts}
43
+ else
44
+ # do nothing
45
+ end
46
+ group = group.with_indifferent_access
47
+ @groups << group
48
+ @groups = @groups.uniq { |i| i[:name] }
49
+ @current_group = group
50
+ block.call(group[:klass])
51
+ @current_group = nil
52
+ end
53
+
54
+ def limit(name=nil, &block)
55
+ raise "Need define group first" if @current_group.nil?
56
+ Power.power(name||@current_group[:name], &block)
57
+ end
58
+
59
+ def collection(verb_or_verbs, &block)
60
+ raise "Need define group first" if @current_group.nil?
61
+ group = @current_group
62
+ behavior = nil
63
+ if block
64
+ behavior = lambda do |user|
65
+ # 在block定义的binding里,注入user这个变量。
66
+ old_binding = block.binding
67
+ old_binding.eval("user=nil;lambda {|v| user = v}").call(user)
68
+ block.call_with_binding(old_binding)
69
+ end
70
+ end
71
+
72
+ if verb_or_verbs.kind_of?(Array)
73
+ verb_or_verbs.each do |verb|
74
+ add_resource(group, verb, group[:klass], 'collection', behavior)
75
+ end
76
+ else
77
+ add_resource(group, verb_or_verbs, group[:klass], 'collection', behavior)
78
+ end
79
+ end
80
+
81
+ def member(verb_or_verbs, &block)
82
+ raise "Need define group first" if @current_group.nil?
83
+ group = @current_group
84
+ behavior = nil
85
+ if block
86
+ behavior = lambda do |user, obj|
87
+ # 在block定义的binding里,注入user这个变量。
88
+ old_binding = block.binding
89
+ old_binding.eval("user=nil;lambda {|v| user = v}").call(user)
90
+ block.call_with_binding(old_binding, obj)
91
+ end
92
+ end
93
+
94
+ if verb_or_verbs.kind_of?(Array)
95
+ verb_or_verbs.each do |verb|
96
+ add_resource(group, verb, group[:klass], 'member', behavior)
97
+ end
98
+ else
99
+ add_resource(group, verb_or_verbs, group[:klass], 'member', behavior)
100
+ end
101
+ end
102
+
103
+ def add_resource(group, verb, object, type, behavior)
104
+ name = "#{verb}_#{object.to_s.underscore}"
105
+ resource = {
106
+ name: name,
107
+ group: group,
108
+ verb: verb,
109
+ object: object,
110
+ type: type,
111
+ behavior: behavior
112
+ }.with_indifferent_access
113
+ @resources.keep_if { |i| i[:name] != name }
114
+ @resources << resource
115
+ end
116
+
117
+ def find_by_name(name)
118
+ @resources.find { |r| r[:name] == name }
119
+ end
120
+
121
+ def grouped_resources
122
+ @grouped_resources ||= @resources.group_by { |i| i[:group] }
123
+ end
124
+
125
+ def my_resources
126
+ @resources
127
+ end
128
+
129
+ def grouped_resources_with_chinese_desc
130
+ grouped_resources.tap do |i|
131
+ i.each do |group, resources|
132
+ group[:chinese_desc] = begin
133
+ name = I18n.t("can_play.class_name.#{group[:name].singularize}", default: '')
134
+ name = group[:klass].model_name.human if name.blank?
135
+ name
136
+ end
137
+ resources.each do |resource|
138
+ resource[:chinese_desc] = I18n.t("can_play.authority_name.#{group[:name].singularize}.#{resource[:verb]}", default: '').presence || I18n.t("can_play.authority_name.common.#{resource[:verb]}")
139
+ end
140
+ end
141
+ i.rehash
142
+ end
143
+ end
144
+ end
145
+ end
@@ -0,0 +1,20 @@
1
+ require 'rails/generators/base'
2
+
3
+ module CanPlay
4
+ module Generators
5
+ class InstallGenerator < Rails::Generators::Base
6
+ source_root File.expand_path("../../templates", __FILE__)
7
+
8
+ desc "Creates a can_play initializer and copy locale files to your application."
9
+
10
+ def copy_initializer
11
+ template "can_play.rb", "config/initializers/can_play.rb"
12
+ end
13
+
14
+ def copy_locale
15
+ copy_file "../../../config/locales/zh-Cn.yml", "config/locales/can_play.zh-CN.yml"
16
+ end
17
+
18
+ end
19
+ end
20
+ end
@@ -0,0 +1,8 @@
1
+ # 在此可设置角色类名称、角色权限中间表等信息。
2
+
3
+ CanPlay::Config.setup do |config|
4
+ config.user_class_name = 'User'
5
+ config.role_class_name = 'Role'
6
+ config.role_resources_middle_class_name = 'RoleResource'
7
+ config.resource_class_name = 'Resource'
8
+ end
metadata ADDED
@@ -0,0 +1,130 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: can_play
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.6
5
+ platform: ruby
6
+ authors:
7
+ - happyming9527
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2015-08-21 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: bundler
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - ~>
18
+ - !ruby/object:Gem::Version
19
+ version: '1.10'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - ~>
25
+ - !ruby/object:Gem::Version
26
+ version: '1.10'
27
+ - !ruby/object:Gem::Dependency
28
+ name: rake
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - ~>
32
+ - !ruby/object:Gem::Version
33
+ version: '10.0'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - ~>
39
+ - !ruby/object:Gem::Version
40
+ version: '10.0'
41
+ - !ruby/object:Gem::Dependency
42
+ name: cancancan
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - ~>
46
+ - !ruby/object:Gem::Version
47
+ version: '1.12'
48
+ type: :runtime
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - ~>
53
+ - !ruby/object:Gem::Version
54
+ version: '1.12'
55
+ - !ruby/object:Gem::Dependency
56
+ name: consul
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - ~>
60
+ - !ruby/object:Gem::Version
61
+ version: '0.12'
62
+ type: :runtime
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - ~>
67
+ - !ruby/object:Gem::Version
68
+ version: '0.12'
69
+ - !ruby/object:Gem::Dependency
70
+ name: ror_hack
71
+ requirement: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - ~>
74
+ - !ruby/object:Gem::Version
75
+ version: '0.1'
76
+ type: :runtime
77
+ prerelease: false
78
+ version_requirements: !ruby/object:Gem::Requirement
79
+ requirements:
80
+ - - ~>
81
+ - !ruby/object:Gem::Version
82
+ version: '0.1'
83
+ description: control user's permissions based on role and resource.
84
+ email:
85
+ - happyming9527@gmail.com
86
+ executables: []
87
+ extensions: []
88
+ extra_rdoc_files: []
89
+ files:
90
+ - .gitignore
91
+ - Gemfile
92
+ - LICENSE.txt
93
+ - README.md
94
+ - Rakefile
95
+ - bin/console
96
+ - bin/setup
97
+ - can_play.gemspec
98
+ - config/locales/zh-CN.yml
99
+ - lib/can_play.rb
100
+ - lib/can_play/ability.rb
101
+ - lib/can_play/controller.rb
102
+ - lib/can_play/power.rb
103
+ - lib/can_play/version.rb
104
+ - lib/generators/can_play/install_generator.rb
105
+ - lib/generators/templates/can_play.rb
106
+ homepage: https://github.com/happyming9527/can_play
107
+ licenses:
108
+ - MIT
109
+ metadata: {}
110
+ post_install_message:
111
+ rdoc_options: []
112
+ require_paths:
113
+ - lib
114
+ required_ruby_version: !ruby/object:Gem::Requirement
115
+ requirements:
116
+ - - '>='
117
+ - !ruby/object:Gem::Version
118
+ version: '0'
119
+ required_rubygems_version: !ruby/object:Gem::Requirement
120
+ requirements:
121
+ - - '>='
122
+ - !ruby/object:Gem::Version
123
+ version: '0'
124
+ requirements: []
125
+ rubyforge_project:
126
+ rubygems_version: 2.4.6
127
+ signing_key:
128
+ specification_version: 4
129
+ summary: a permission system.
130
+ test_files: []