bsm-sso-client 0.7.4 → 0.7.5
Sign up to get free protection for your applications and to get access to all the features.
- data/lib/bsm/sso/client.rb +1 -0
- data/lib/bsm/sso/client/ability.rb +78 -0
- metadata +22 -4
data/lib/bsm/sso/client.rb
CHANGED
@@ -0,0 +1,78 @@
|
|
1
|
+
begin
|
2
|
+
require 'cancan/ability'
|
3
|
+
rescue LoadError => e
|
4
|
+
warn "\n [!] Please install `cancan` Gem to use the Ability module\n"
|
5
|
+
raise
|
6
|
+
end
|
7
|
+
|
8
|
+
module Bsm::Sso::Client::Ability
|
9
|
+
extend ActiveSupport::Concern
|
10
|
+
|
11
|
+
included do
|
12
|
+
include CanCan::Ability
|
13
|
+
end
|
14
|
+
|
15
|
+
module ClassMethods
|
16
|
+
|
17
|
+
# @return [Hash] roles, scoped by user type
|
18
|
+
def roles
|
19
|
+
private_instance_methods(false).inject({}) do |result, name|
|
20
|
+
prefix, scope, name = name.to_s.split('__')
|
21
|
+
next result unless prefix == "as" && scope && name
|
22
|
+
|
23
|
+
result[scope.to_sym] ||= []
|
24
|
+
result[scope.to_sym] << name
|
25
|
+
result
|
26
|
+
end
|
27
|
+
end
|
28
|
+
|
29
|
+
# Ability definition helper
|
30
|
+
# @param [Symbol] scope the user scope
|
31
|
+
# @param [String] name the role name
|
32
|
+
def as(scope, name, &block)
|
33
|
+
method_name = :"as__#{scope}__#{name}"
|
34
|
+
|
35
|
+
define_method(method_name) do
|
36
|
+
return false if self.scope != scope || applied.include?(name.to_s)
|
37
|
+
applied.add(name.to_s)
|
38
|
+
instance_eval(&block)
|
39
|
+
true
|
40
|
+
end
|
41
|
+
private method_name
|
42
|
+
end
|
43
|
+
|
44
|
+
end
|
45
|
+
|
46
|
+
# @attr_reader [User] current user record
|
47
|
+
attr_reader :user
|
48
|
+
|
49
|
+
# Construstor
|
50
|
+
# @param [User] current user record
|
51
|
+
def initialize(user)
|
52
|
+
@user = user
|
53
|
+
@user.roles.each do |name|
|
54
|
+
same_as(name)
|
55
|
+
end
|
56
|
+
if @user.level >= 90
|
57
|
+
same_as(:administrator)
|
58
|
+
end
|
59
|
+
end
|
60
|
+
|
61
|
+
# @return [Symbol] the user scope
|
62
|
+
def scope
|
63
|
+
@scope ||= (user.respond_to?(:employee?) && user.employee? ? :employee : :client)
|
64
|
+
end
|
65
|
+
|
66
|
+
# @return [Set] applied roles
|
67
|
+
def applied
|
68
|
+
@applied ||= Set.new
|
69
|
+
end
|
70
|
+
|
71
|
+
# Runs a role method
|
72
|
+
# @param [String] name the role name
|
73
|
+
def same_as(name)
|
74
|
+
method = :"as__#{scope}__#{name}"
|
75
|
+
send(method) if respond_to?(method, true)
|
76
|
+
end
|
77
|
+
|
78
|
+
end
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: bsm-sso-client
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.7.
|
4
|
+
version: 0.7.5
|
5
5
|
prerelease:
|
6
6
|
platform: ruby
|
7
7
|
authors:
|
@@ -9,7 +9,7 @@ authors:
|
|
9
9
|
autorequire:
|
10
10
|
bindir: bin
|
11
11
|
cert_chain: []
|
12
|
-
date: 2012-08-
|
12
|
+
date: 2012-08-30 00:00:00.000000000 Z
|
13
13
|
dependencies:
|
14
14
|
- !ruby/object:Gem::Dependency
|
15
15
|
name: railties
|
@@ -82,7 +82,7 @@ dependencies:
|
|
82
82
|
requirements:
|
83
83
|
- - ! '>='
|
84
84
|
- !ruby/object:Gem::Version
|
85
|
-
version: 0.
|
85
|
+
version: 0.16.0
|
86
86
|
type: :runtime
|
87
87
|
prerelease: false
|
88
88
|
version_requirements: !ruby/object:Gem::Requirement
|
@@ -90,7 +90,23 @@ dependencies:
|
|
90
90
|
requirements:
|
91
91
|
- - ! '>='
|
92
92
|
- !ruby/object:Gem::Version
|
93
|
-
version: 0.
|
93
|
+
version: 0.16.0
|
94
|
+
- !ruby/object:Gem::Dependency
|
95
|
+
name: cancan
|
96
|
+
requirement: !ruby/object:Gem::Requirement
|
97
|
+
none: false
|
98
|
+
requirements:
|
99
|
+
- - ~>
|
100
|
+
- !ruby/object:Gem::Version
|
101
|
+
version: 1.6.8
|
102
|
+
type: :development
|
103
|
+
prerelease: false
|
104
|
+
version_requirements: !ruby/object:Gem::Requirement
|
105
|
+
none: false
|
106
|
+
requirements:
|
107
|
+
- - ~>
|
108
|
+
- !ruby/object:Gem::Version
|
109
|
+
version: 1.6.8
|
94
110
|
- !ruby/object:Gem::Dependency
|
95
111
|
name: activerecord
|
96
112
|
requirement: !ruby/object:Gem::Requirement
|
@@ -215,6 +231,7 @@ files:
|
|
215
231
|
- lib/bsm/sso/client/strategies/http_auth.rb
|
216
232
|
- lib/bsm/sso/client/cached.rb
|
217
233
|
- lib/bsm/sso/client/failure_app.rb
|
234
|
+
- lib/bsm/sso/client/ability.rb
|
218
235
|
- lib/bsm/sso/client/abstract_resource.rb
|
219
236
|
- lib/bsm/sso/client/railtie.rb
|
220
237
|
- lib/bsm/sso/client/user_methods.rb
|
@@ -250,3 +267,4 @@ signing_key:
|
|
250
267
|
specification_version: 3
|
251
268
|
summary: BSM's internal SSO client
|
252
269
|
test_files: []
|
270
|
+
has_rdoc:
|