esp-auth 1.0.0
Sign up to get free protection for your applications and to get access to all the features.
- data/MIT-LICENSE +20 -0
- data/README.rdoc +28 -0
- data/Rakefile +28 -0
- data/app/assets/images/esp_auth/gh_icons.png +0 -0
- data/app/assets/images/esp_auth/inline_error_arrow.png +0 -0
- data/app/assets/images/esp_auth/wood.jpg +0 -0
- data/app/assets/javascripts/esp_auth/application.js +4 -0
- data/app/assets/javascripts/esp_auth/jquery.noisy.min.js +3 -0
- data/app/assets/javascripts/esp_auth/permissions.js +62 -0
- data/app/assets/stylesheets/esp_auth/application.css +11 -0
- data/app/assets/stylesheets/esp_auth/buttons.sass +300 -0
- data/app/assets/stylesheets/esp_auth/jquery_ui.sass +1493 -0
- data/app/assets/stylesheets/esp_auth/pagination.sass +19 -0
- data/app/assets/stylesheets/esp_auth/permissions.sass +150 -0
- data/app/assets/stylesheets/esp_auth/shared.sass +84 -0
- data/app/controllers/esp_auth/application_controller.rb +11 -0
- data/app/controllers/esp_auth/omniauth_callbacks_controller.rb +11 -0
- data/app/controllers/esp_auth/permissions_controller.rb +13 -0
- data/app/controllers/esp_auth/sessions_controller.rb +16 -0
- data/app/controllers/esp_auth/users_controller.rb +24 -0
- data/app/models/user_search.rb +13 -0
- data/app/views/esp_auth/permissions/new.html.erb +23 -0
- data/app/views/esp_auth/shared/_footer.html.erb +12 -0
- data/app/views/esp_auth/shared/_header.html.erb +24 -0
- data/app/views/esp_auth/users/index.html.erb +53 -0
- data/app/views/layouts/esp_auth/application.html.erb +18 -0
- data/config/initializers/devise.rb +223 -0
- data/config/locales/ru.yml +35 -0
- data/config/routes.rb +25 -0
- data/lib/esp-auth.rb +19 -0
- data/lib/esp_auth/engine.rb +41 -0
- data/lib/esp_auth/spec_helper.rb +68 -0
- data/lib/esp_auth/version.rb +3 -0
- data/lib/generators/esp_auth/install/install_generator.rb +49 -0
- data/lib/generators/esp_auth/install/templates/app/controllers/manage/application_controller.rb +3 -0
- data/lib/generators/esp_auth/install/templates/app/models/ability.rb +41 -0
- data/lib/generators/esp_auth/install/templates/app/models/context.rb +27 -0
- data/lib/generators/esp_auth/install/templates/app/models/permission.rb +69 -0
- data/lib/generators/esp_auth/install/templates/app/models/subcontext.rb +21 -0
- data/lib/generators/esp_auth/install/templates/app/models/user.rb +67 -0
- data/lib/generators/esp_auth/install/templates/config/locales/permissions_enum.ru.yml +6 -0
- data/lib/generators/esp_auth/install/templates/config/schedule.rb +5 -0
- data/lib/generators/esp_auth/install/templates/db/migrate/esp_auth_create_contexts.rb +12 -0
- data/lib/generators/esp_auth/install/templates/db/migrate/esp_auth_create_permissions.rb +11 -0
- data/lib/generators/esp_auth/install/templates/db/migrate/esp_auth_create_subcontexts.rb +9 -0
- data/lib/generators/esp_auth/install/templates/db/migrate/esp_auth_create_users.rb +29 -0
- data/lib/generators/esp_auth/install/templates/db/seeds.rb +4 -0
- data/lib/generators/esp_auth/install/templates/spec/models/ability_spec.rb +83 -0
- data/lib/omniauth/strategies/identity.rb +15 -0
- data/lib/tasks/sync.rake +17 -0
- metadata +453 -0
@@ -0,0 +1,223 @@
|
|
1
|
+
# Use this hook to configure devise mailer, warden hooks and so forth.
|
2
|
+
# Many of these configuration options can be set straight in your model.
|
3
|
+
Devise.setup do |config|
|
4
|
+
# ==> Mailer Configuration
|
5
|
+
# Configure the e-mail address which will be shown in Devise::Mailer,
|
6
|
+
# note that it will be overwritten if you use your own mailer class with default "from" parameter.
|
7
|
+
# config.mailer_sender = "please-change-me-at-config-initializers-devise@example.com"
|
8
|
+
|
9
|
+
# Configure the class responsible to send e-mails.
|
10
|
+
# config.mailer = "Devise::Mailer"
|
11
|
+
|
12
|
+
# Automatically apply schema changes in tableless databases
|
13
|
+
config.apply_schema = false
|
14
|
+
|
15
|
+
# ==> ORM configuration
|
16
|
+
# Load and configure the ORM. Supports :active_record (default) and
|
17
|
+
# :mongoid (bson_ext recommended) by default. Other ORMs may be
|
18
|
+
# available as additional gems.
|
19
|
+
require 'devise/orm/active_record'
|
20
|
+
|
21
|
+
# ==> Configuration for any authentication mechanism
|
22
|
+
# Configure which keys are used when authenticating a user. The default is
|
23
|
+
# just :email. You can configure it to use [:username, :subdomain], so for
|
24
|
+
# authenticating a user, both parameters are required. Remember that those
|
25
|
+
# parameters are used only when authenticating and not when retrieving from
|
26
|
+
# session. If you need permissions, you should implement that in a before filter.
|
27
|
+
# You can also supply a hash where the value is a boolean determining whether
|
28
|
+
# or not authentication should be aborted when the value is not present.
|
29
|
+
# config.authentication_keys = [ :email ]
|
30
|
+
|
31
|
+
# Configure parameters from the request object used for authentication. Each entry
|
32
|
+
# given should be a request method and it will automatically be passed to the
|
33
|
+
# find_for_authentication method and considered in your model lookup. For instance,
|
34
|
+
# if you set :request_keys to [:subdomain], :subdomain will be used on authentication.
|
35
|
+
# The same considerations mentioned for authentication_keys also apply to request_keys.
|
36
|
+
# config.request_keys = []
|
37
|
+
|
38
|
+
# Configure which authentication keys should be case-insensitive.
|
39
|
+
# These keys will be downcased upon creating or modifying a user and when used
|
40
|
+
# to authenticate or find a user. Default is :email.
|
41
|
+
config.case_insensitive_keys = []
|
42
|
+
|
43
|
+
# Configure which authentication keys should have whitespace stripped.
|
44
|
+
# These keys will have whitespace before and after removed upon creating or
|
45
|
+
# modifying a user and when used to authenticate or find a user. Default is :email.
|
46
|
+
config.strip_whitespace_keys = []
|
47
|
+
|
48
|
+
# Tell if authentication through request.params is enabled. True by default.
|
49
|
+
# It can be set to an array that will enable params authentication only for the
|
50
|
+
# given strategies, for example, `config.params_authenticatable = [:database]` will
|
51
|
+
# enable it only for database (email + password) authentication.
|
52
|
+
# config.params_authenticatable = true
|
53
|
+
|
54
|
+
# Tell if authentication through HTTP Basic Auth is enabled. False by default.
|
55
|
+
# It can be set to an array that will enable http authentication only for the
|
56
|
+
# given strategies, for example, `config.http_authenticatable = [:token]` will
|
57
|
+
# enable it only for token authentication.
|
58
|
+
# config.http_authenticatable = false
|
59
|
+
|
60
|
+
# If http headers should be returned for AJAX requests. True by default.
|
61
|
+
# config.http_authenticatable_on_xhr = true
|
62
|
+
|
63
|
+
# The realm used in Http Basic Authentication. "Application" by default.
|
64
|
+
# config.http_authentication_realm = "Application"
|
65
|
+
|
66
|
+
# It will change confirmation, password recovery and other workflows
|
67
|
+
# to behave the same regardless if the e-mail provided was right or wrong.
|
68
|
+
# Does not affect registerable.
|
69
|
+
# config.paranoid = true
|
70
|
+
|
71
|
+
# By default Devise will store the user in session. You can skip storage for
|
72
|
+
# :http_auth and :token_auth by adding those symbols to the array below.
|
73
|
+
# Notice that if you are skipping storage for all authentication paths, you
|
74
|
+
# may want to disable generating routes to Devise's sessions controller by
|
75
|
+
# passing :skip => :sessions to `devise_for` in your config/routes.rb
|
76
|
+
config.skip_session_storage = [:http_auth]
|
77
|
+
|
78
|
+
# ==> Configuration for :database_authenticatable
|
79
|
+
# For bcrypt, this is the cost for hashing the password and defaults to 10. If
|
80
|
+
# using other encryptors, it sets how many times you want the password re-encrypted.
|
81
|
+
#
|
82
|
+
# Limiting the stretches to just one in testing will increase the performance of
|
83
|
+
# your test suite dramatically. However, it is STRONGLY RECOMMENDED to not use
|
84
|
+
# a value less than 10 in other environments.
|
85
|
+
config.stretches = Rails.env.test? ? 1 : 10
|
86
|
+
|
87
|
+
# Setup a pepper to generate the encrypted password.
|
88
|
+
# config.pepper = "ae446f37fc90fcaf3e69e6778874d32d05bdc3b515a5f2029ae43947a3596bcce76380251d9b77019827a7c16439fe7efddcb46988a4eda678f570b6488f5c57"
|
89
|
+
|
90
|
+
# ==> Configuration for :confirmable
|
91
|
+
# A period that the user is allowed to access the website even without
|
92
|
+
# confirming his account. For instance, if set to 2.days, the user will be
|
93
|
+
# able to access the website for two days without confirming his account,
|
94
|
+
# access will be blocked just in the third day. Default is 0.days, meaning
|
95
|
+
# the user cannot access the website without confirming his account.
|
96
|
+
# config.allow_unconfirmed_access_for = 2.days
|
97
|
+
|
98
|
+
# If true, requires any email changes to be confirmed (exctly the same way as
|
99
|
+
# initial account confirmation) to be applied. Requires additional unconfirmed_email
|
100
|
+
# db field (see migrations). Until confirmed new email is stored in
|
101
|
+
# unconfirmed email column, and copied to email column on successful confirmation.
|
102
|
+
config.reconfirmable = true
|
103
|
+
|
104
|
+
# Defines which key will be used when confirming an account
|
105
|
+
# config.confirmation_keys = [ :email ]
|
106
|
+
|
107
|
+
# ==> Configuration for :rememberable
|
108
|
+
# The time the user will be remembered without asking for credentials again.
|
109
|
+
# config.remember_for = 2.weeks
|
110
|
+
|
111
|
+
# If true, extends the user's remember period when remembered via cookie.
|
112
|
+
# config.extend_remember_period = false
|
113
|
+
|
114
|
+
# If true, uses the password salt as remember token. This should be turned
|
115
|
+
# to false if you are not using database authenticatable.
|
116
|
+
config.use_salt_as_remember_token = true
|
117
|
+
|
118
|
+
# Options to be passed to the created cookie. For instance, you can set
|
119
|
+
# :secure => true in order to force SSL only cookies.
|
120
|
+
# config.cookie_options = {}
|
121
|
+
|
122
|
+
# ==> Configuration for :validatable
|
123
|
+
# Range for password length. Default is 6..128.
|
124
|
+
# config.password_length = 6..128
|
125
|
+
|
126
|
+
# Email regex used to validate email formats. It simply asserts that
|
127
|
+
# an one (and only one) @ exists in the given string. This is mainly
|
128
|
+
# to give user feedback and not to assert the e-mail validity.
|
129
|
+
# config.email_regexp = /\A[^@]+@[^@]+\z/
|
130
|
+
|
131
|
+
# ==> Configuration for :timeoutable
|
132
|
+
# The time you want to timeout the user session without activity. After this
|
133
|
+
# time the user will be asked for credentials again. Default is 30 minutes.
|
134
|
+
# config.timeout_in = 30.minutes
|
135
|
+
|
136
|
+
# ==> Configuration for :lockable
|
137
|
+
# Defines which strategy will be used to lock an account.
|
138
|
+
# :failed_attempts = Locks an account after a number of failed attempts to sign in.
|
139
|
+
# :none = No lock strategy. You should handle locking by yourself.
|
140
|
+
# config.lock_strategy = :failed_attempts
|
141
|
+
|
142
|
+
# Defines which key will be used when locking and unlocking an account
|
143
|
+
# config.unlock_keys = [ :email ]
|
144
|
+
|
145
|
+
# Defines which strategy will be used to unlock an account.
|
146
|
+
# :email = Sends an unlock link to the user email
|
147
|
+
# :time = Re-enables login after a certain amount of time (see :unlock_in below)
|
148
|
+
# :both = Enables both strategies
|
149
|
+
# :none = No unlock strategy. You should handle unlocking by yourself.
|
150
|
+
# config.unlock_strategy = :both
|
151
|
+
|
152
|
+
# Number of authentication tries before locking an account if lock_strategy
|
153
|
+
# is failed attempts.
|
154
|
+
# config.maximum_attempts = 20
|
155
|
+
|
156
|
+
# Time interval to unlock the account if :time is enabled as unlock_strategy.
|
157
|
+
# config.unlock_in = 1.hour
|
158
|
+
|
159
|
+
# ==> Configuration for :recoverable
|
160
|
+
#
|
161
|
+
# Defines which key will be used when recovering the password for an account
|
162
|
+
# config.reset_password_keys = [ :email ]
|
163
|
+
|
164
|
+
# Time interval you can reset your password with a reset password key.
|
165
|
+
# Don't put a too small interval or your users won't have the time to
|
166
|
+
# change their passwords.
|
167
|
+
config.reset_password_within = 6.hours
|
168
|
+
|
169
|
+
# ==> Configuration for :encryptable
|
170
|
+
# Allow you to use another encryption algorithm besides bcrypt (default). You can use
|
171
|
+
# :sha1, :sha512 or encryptors from others authentication tools as :clearance_sha1,
|
172
|
+
# :authlogic_sha512 (then you should set stretches above to 20 for default behavior)
|
173
|
+
# and :restful_authentication_sha1 (then you should set stretches to 10, and copy
|
174
|
+
# REST_AUTH_SITE_KEY to pepper)
|
175
|
+
# config.encryptor = :sha512
|
176
|
+
|
177
|
+
# ==> Configuration for :token_authenticatable
|
178
|
+
# Defines name of the authentication token params key
|
179
|
+
# config.token_authentication_key = :auth_token
|
180
|
+
|
181
|
+
# ==> Scopes configuration
|
182
|
+
# Turn scoped views on. Before rendering "sessions/new", it will first check for
|
183
|
+
# "users/sessions/new". It's turned off by default because it's slower if you
|
184
|
+
# are using only default views.
|
185
|
+
# config.scoped_views = false
|
186
|
+
|
187
|
+
# Configure the default scope given to Warden. By default it's the first
|
188
|
+
# devise role declared in your routes (usually :user).
|
189
|
+
# config.default_scope = :user
|
190
|
+
|
191
|
+
# Configure sign_out behavior.
|
192
|
+
# Sign_out action can be scoped (i.e. /users/sign_out affects only :user scope).
|
193
|
+
# The default is true, which means any logout action will sign out all active scopes.
|
194
|
+
# config.sign_out_all_scopes = true
|
195
|
+
|
196
|
+
# ==> Navigation configuration
|
197
|
+
# Lists the formats that should be treated as navigational. Formats like
|
198
|
+
# :html, should redirect to the sign in page when the user does not have
|
199
|
+
# access, but formats like :xml or :json, should return 401.
|
200
|
+
#
|
201
|
+
# If you have any extra navigational formats, like :iphone or :mobile, you
|
202
|
+
# should add them to the navigational formats lists.
|
203
|
+
#
|
204
|
+
# The "*/*" below is required to match Internet Explorer requests.
|
205
|
+
# config.navigational_formats = ["*/*", :html]
|
206
|
+
|
207
|
+
# The default HTTP method used to sign out a resource. Default is :delete.
|
208
|
+
config.sign_out_via = :delete
|
209
|
+
|
210
|
+
# ==> OmniAuth
|
211
|
+
# Add a new OmniAuth provider. Check the wiki for more information on setting
|
212
|
+
# up on your models and hooks.
|
213
|
+
# config.omniauth :github, 'APP_ID', 'APP_SECRET', :scope => 'user,public_repo'
|
214
|
+
|
215
|
+
# ==> Warden configuration
|
216
|
+
# If you want to use other strategies, that are not supported by Devise, or
|
217
|
+
# change the failure app, you can configure them inside the config.warden block.
|
218
|
+
#
|
219
|
+
# config.warden do |manager|
|
220
|
+
# manager.intercept_401 = false
|
221
|
+
# manager.default_strategies(:scope => :user).unshift :some_external_strategy
|
222
|
+
# end
|
223
|
+
end
|
@@ -0,0 +1,35 @@
|
|
1
|
+
ru:
|
2
|
+
activerecord:
|
3
|
+
attributes:
|
4
|
+
permission:
|
5
|
+
polimorphic_context: Контекст
|
6
|
+
context: Категория
|
7
|
+
role: Роль
|
8
|
+
user_search: Пользователь
|
9
|
+
|
10
|
+
models:
|
11
|
+
permission: Право доступа
|
12
|
+
|
13
|
+
views:
|
14
|
+
pagination:
|
15
|
+
next: "→"
|
16
|
+
last: Последняя страница
|
17
|
+
previous: "←"
|
18
|
+
first: Первая страница
|
19
|
+
truncate: ...
|
20
|
+
|
21
|
+
permissions:
|
22
|
+
'Are you sure?': Вы уверены, в том что хотите удалить запись?
|
23
|
+
'You are sign in as': Вы вошли как
|
24
|
+
'You are adding role to': Вы добавляете роль для
|
25
|
+
back_to_manage: Назад в раздел управления контентом
|
26
|
+
cancel: Отмена
|
27
|
+
create: Создать
|
28
|
+
create_permission: Создать право доступа
|
29
|
+
delete: Удалить
|
30
|
+
new: Добавить право доступа
|
31
|
+
not_selected: -- не выбрано --
|
32
|
+
search: Поиск
|
33
|
+
show_permissions: Показать права доступа
|
34
|
+
sign_out: Выход
|
35
|
+
title: Управление правами доступа
|
data/config/routes.rb
ADDED
@@ -0,0 +1,25 @@
|
|
1
|
+
EspAuth::Engine.routes.draw do
|
2
|
+
resources :permissions, :only => [:new, :create, :destroy]
|
3
|
+
|
4
|
+
resources :users, :only => :index do
|
5
|
+
resources :permissions, :only => [:new, :create]
|
6
|
+
end
|
7
|
+
|
8
|
+
match '/users/search' => "users#search"
|
9
|
+
|
10
|
+
get 'sign_out' => 'sessions#destroy', :as => :destroy_user_session
|
11
|
+
|
12
|
+
root :to => 'users#index'
|
13
|
+
end
|
14
|
+
|
15
|
+
Rails.application.routes.draw do
|
16
|
+
devise_for :users, :path => 'auth', controllers: {omniauth_callbacks:'esp_auth/omniauth_callbacks'}, :skip => [:sessions]
|
17
|
+
|
18
|
+
devise_scope :users do
|
19
|
+
get 'sign_in' => redirect('/auth/auth/identity'), :as => :new_user_session
|
20
|
+
end
|
21
|
+
|
22
|
+
mount EspAuth::Engine => '/auth'
|
23
|
+
|
24
|
+
end rescue NameError
|
25
|
+
|
data/lib/esp-auth.rb
ADDED
@@ -0,0 +1,19 @@
|
|
1
|
+
require "esp_auth/engine"
|
2
|
+
|
3
|
+
require 'ancestry'
|
4
|
+
require 'cancan'
|
5
|
+
require 'curb'
|
6
|
+
require 'devise'
|
7
|
+
require 'default_value_for'
|
8
|
+
require 'has_enum'
|
9
|
+
require 'has_scope'
|
10
|
+
require 'has_searcher'
|
11
|
+
require 'inherited_resources'
|
12
|
+
require 'jquery-rails'
|
13
|
+
require 'kaminari'
|
14
|
+
require 'sprockets'
|
15
|
+
require 'sass-rails'
|
16
|
+
require 'sunspot_rails'
|
17
|
+
|
18
|
+
module EspAuth
|
19
|
+
end
|
@@ -0,0 +1,41 @@
|
|
1
|
+
module EspAuth
|
2
|
+
class Engine < Rails::Engine
|
3
|
+
isolate_namespace EspAuth
|
4
|
+
|
5
|
+
config.after_initialize do
|
6
|
+
begin
|
7
|
+
Settings.resolve!
|
8
|
+
rescue => e
|
9
|
+
puts "WARNING! #{e.message}"
|
10
|
+
end
|
11
|
+
end
|
12
|
+
|
13
|
+
initializer "sso_client.devise", :before => 'devise.omniauth' do |app|
|
14
|
+
require File.expand_path("../../../lib/omniauth/strategies/identity", __FILE__)
|
15
|
+
|
16
|
+
Settings.define 'sso.url', :env_var => 'SSO_URL', :required => true
|
17
|
+
Settings.define 'sso.key', :env_var => 'SSO_KEY', :required => true
|
18
|
+
Settings.define 'sso.secret', :env_var => 'SSO_SECRET', :required => true
|
19
|
+
|
20
|
+
Devise.setup do |config|
|
21
|
+
config.omniauth :identity, Settings['sso.key'], Settings['sso.secret'], :client_options => {:site => Settings['sso.url']}
|
22
|
+
end
|
23
|
+
end
|
24
|
+
|
25
|
+
config.to_prepare do
|
26
|
+
ActionController::Base.class_eval do
|
27
|
+
def self.esp_load_and_authorize_resource
|
28
|
+
inherit_resources
|
29
|
+
load_and_authorize_resource
|
30
|
+
|
31
|
+
before_filter :authenticate_user!
|
32
|
+
before_filter :authorize_user_can_manage_application!
|
33
|
+
end
|
34
|
+
protected
|
35
|
+
def authorize_user_can_manage_application!
|
36
|
+
authorize! :manage, :application
|
37
|
+
end
|
38
|
+
end
|
39
|
+
end
|
40
|
+
end
|
41
|
+
end
|
@@ -0,0 +1,68 @@
|
|
1
|
+
module EspAuth
|
2
|
+
module SpecHelper
|
3
|
+
|
4
|
+
def ability_for(user)
|
5
|
+
Ability.new(user)
|
6
|
+
end
|
7
|
+
|
8
|
+
def user
|
9
|
+
@user ||= User.create! :uid => 1, :name => 'User'
|
10
|
+
end
|
11
|
+
|
12
|
+
def another_user
|
13
|
+
@another_user ||= User.create! :uid => 2, :name => 'Another user'
|
14
|
+
end
|
15
|
+
|
16
|
+
def create_context(parent=nil)
|
17
|
+
Context.create! :title => 'context', :parent => parent
|
18
|
+
end
|
19
|
+
|
20
|
+
def create_subcontext(context)
|
21
|
+
Subcontext.create! :title => 'subcontext', :context => context
|
22
|
+
end
|
23
|
+
|
24
|
+
def root
|
25
|
+
@root ||= create_context
|
26
|
+
end
|
27
|
+
|
28
|
+
def child_1
|
29
|
+
@child_1 ||= create_context root
|
30
|
+
end
|
31
|
+
|
32
|
+
def child_1_1
|
33
|
+
@child_1_1 ||= create_context child_1
|
34
|
+
end
|
35
|
+
|
36
|
+
def child_1_2
|
37
|
+
@child_1_2 ||= create_context child_1
|
38
|
+
end
|
39
|
+
|
40
|
+
def child_2
|
41
|
+
@child_2 ||= create_context root
|
42
|
+
end
|
43
|
+
|
44
|
+
def subcontext(context)
|
45
|
+
@subcontext ||= create_subcontext(context)
|
46
|
+
end
|
47
|
+
|
48
|
+
def another_subcontext(context)
|
49
|
+
@another_subcontext ||= create_subcontext(context)
|
50
|
+
end
|
51
|
+
|
52
|
+
Permission.enums[:role].each do | role |
|
53
|
+
define_method "#{role}_of" do |context|
|
54
|
+
user.tap do | user |
|
55
|
+
user.permissions.create! :context => context, :role => role
|
56
|
+
end
|
57
|
+
end
|
58
|
+
end
|
59
|
+
|
60
|
+
Permission.enums[:role].each do | role |
|
61
|
+
define_method "another_#{role}_of" do |context|
|
62
|
+
another_user.tap do | another_user |
|
63
|
+
another_user.permissions.create! :context => context, :role => role
|
64
|
+
end
|
65
|
+
end
|
66
|
+
end
|
67
|
+
end
|
68
|
+
end
|
@@ -0,0 +1,49 @@
|
|
1
|
+
require 'rails/generators/migration'
|
2
|
+
|
3
|
+
module EspAuth
|
4
|
+
module Generators
|
5
|
+
class InstallGenerator < Rails::Generators::Base
|
6
|
+
include Rails::Generators::Migration
|
7
|
+
|
8
|
+
source_root File.expand_path('../templates', __FILE__)
|
9
|
+
|
10
|
+
def self.next_migration_number(dirname)
|
11
|
+
@number ||= Time.now.strftime('%Y%m%d%H%M%S').to_i
|
12
|
+
@number += 1
|
13
|
+
end
|
14
|
+
|
15
|
+
def create_models
|
16
|
+
template 'app/models/ability.rb'
|
17
|
+
template 'app/models/user.rb'
|
18
|
+
template 'app/models/context.rb'
|
19
|
+
template 'app/models/permission.rb'
|
20
|
+
template 'app/models/subcontext.rb'
|
21
|
+
end
|
22
|
+
|
23
|
+
def create_controllers
|
24
|
+
template 'app/controllers/manage/application_controller.rb'
|
25
|
+
end
|
26
|
+
|
27
|
+
def create_config
|
28
|
+
template 'config/schedule.rb'
|
29
|
+
template 'config/locales/permissions_enum.ru.yml'
|
30
|
+
end
|
31
|
+
|
32
|
+
def create_seeds
|
33
|
+
template 'db/seeds.rb'
|
34
|
+
end
|
35
|
+
|
36
|
+
def create_specs
|
37
|
+
template 'spec/models/ability_spec.rb'
|
38
|
+
end
|
39
|
+
|
40
|
+
def create_migrations
|
41
|
+
migration_template 'db/migrate/esp_auth_create_users.rb'
|
42
|
+
migration_template 'db/migrate/esp_auth_create_contexts.rb'
|
43
|
+
migration_template 'db/migrate/esp_auth_create_permissions.rb'
|
44
|
+
migration_template 'db/migrate/esp_auth_create_subcontexts.rb'
|
45
|
+
end
|
46
|
+
|
47
|
+
end
|
48
|
+
end
|
49
|
+
end
|
@@ -0,0 +1,41 @@
|
|
1
|
+
class Ability
|
2
|
+
include CanCan::Ability
|
3
|
+
|
4
|
+
def initialize(user)
|
5
|
+
return unless user
|
6
|
+
|
7
|
+
## common
|
8
|
+
can :manage, Context do | context |
|
9
|
+
user.manager_of? context
|
10
|
+
end
|
11
|
+
|
12
|
+
can :manage, Permission do | permission |
|
13
|
+
permission.context && user.manager_of?(permission.context)
|
14
|
+
end
|
15
|
+
|
16
|
+
can [:new, :create], Permission do | permission |
|
17
|
+
!permission.context && user.manager?
|
18
|
+
end
|
19
|
+
|
20
|
+
can [:search, :index], User do
|
21
|
+
user.manager?
|
22
|
+
end
|
23
|
+
|
24
|
+
can :manage, :application do
|
25
|
+
user.permissions.exists?
|
26
|
+
end
|
27
|
+
|
28
|
+
can :manage, :permissions do
|
29
|
+
user.manager?
|
30
|
+
end
|
31
|
+
|
32
|
+
## app specific
|
33
|
+
can :manage, Subcontext do | subcontext |
|
34
|
+
user.manager_of? subcontext.context
|
35
|
+
end
|
36
|
+
|
37
|
+
can :manage, Subcontext do | subcontext |
|
38
|
+
user.manager_of? subcontext
|
39
|
+
end
|
40
|
+
end
|
41
|
+
end
|
@@ -0,0 +1,27 @@
|
|
1
|
+
class Context < ActiveRecord::Base
|
2
|
+
|
3
|
+
default_scope order('weight')
|
4
|
+
|
5
|
+
attr_accessible :id, :title, :ancestry, :weight, :parent
|
6
|
+
|
7
|
+
has_many :subcontexts
|
8
|
+
has_many :permissions, :as => :context
|
9
|
+
|
10
|
+
|
11
|
+
alias_attribute :to_s, :title
|
12
|
+
|
13
|
+
has_ancestry
|
14
|
+
|
15
|
+
end
|
16
|
+
# == Schema Information
|
17
|
+
#
|
18
|
+
# Table name: contexts
|
19
|
+
#
|
20
|
+
# id :integer not null, primary key
|
21
|
+
# title :string(255)
|
22
|
+
# ancestry :string(255)
|
23
|
+
# weight :string(255)
|
24
|
+
# created_at :datetime not null
|
25
|
+
# updated_at :datetime not null
|
26
|
+
#
|
27
|
+
|
@@ -0,0 +1,69 @@
|
|
1
|
+
class Permission < ActiveRecord::Base
|
2
|
+
attr_accessor :user_search, :user_uid, :user_name, :user_email, :polimorphic_context
|
3
|
+
|
4
|
+
belongs_to :context, :polymorphic => true
|
5
|
+
belongs_to :user
|
6
|
+
|
7
|
+
scope :for_role, ->(role) { where(:role => role) }
|
8
|
+
scope :for_context_ancestors, ->(context, ids=context.ancestor_ids) do
|
9
|
+
where(:context_id => ids,
|
10
|
+
:context_type => (context.class.ancestors - context.class.included_modules).map(&:name))
|
11
|
+
end
|
12
|
+
scope :for_context, ->(context) { where(:context_id => context.id, :context_type => context.class) }
|
13
|
+
|
14
|
+
scope :for_context_and_ancestors, ->(context) do
|
15
|
+
if context.respond_to?(:ancestor_ids)
|
16
|
+
for_context_ancestors(context, context.ancestor_ids + [context.id])
|
17
|
+
else
|
18
|
+
for_context(context)
|
19
|
+
end
|
20
|
+
end
|
21
|
+
|
22
|
+
after_initialize :set_user, :if => :user_uid_present?
|
23
|
+
after_initialize :set_context, :if => :polimorphic_context_present?
|
24
|
+
|
25
|
+
after_create :user_index!
|
26
|
+
after_destroy :user_index!
|
27
|
+
|
28
|
+
validates_presence_of :polimorphic_context, :unless => :context
|
29
|
+
|
30
|
+
validates_presence_of :role, :user, :context
|
31
|
+
|
32
|
+
validates_presence_of :user_search, :unless => :user
|
33
|
+
|
34
|
+
validates_uniqueness_of :role, :scope => [:user_id, :context_id, :context_type]
|
35
|
+
|
36
|
+
has_enum :role
|
37
|
+
|
38
|
+
|
39
|
+
private
|
40
|
+
|
41
|
+
def set_user
|
42
|
+
self.user = User.find_or_initialize_by_uid(user_uid).tap do |user|
|
43
|
+
user.update_attributes :name => user_name, :email => user_email
|
44
|
+
end
|
45
|
+
end
|
46
|
+
|
47
|
+
def set_context
|
48
|
+
underscored_context_type, self.context_id = polimorphic_context.match(/(\w+)_(\d+)/)[1..2]
|
49
|
+
self.context_type = underscored_context_type.camelize
|
50
|
+
end
|
51
|
+
|
52
|
+
delegate :index!, :to => :user, :prefix => true
|
53
|
+
delegate :present?, :to => :polimorphic_context, :prefix => true
|
54
|
+
delegate :present?, :to => :user_uid, :prefix => true
|
55
|
+
end
|
56
|
+
|
57
|
+
# == Schema Information
|
58
|
+
#
|
59
|
+
# Table name: permissions
|
60
|
+
#
|
61
|
+
# id :integer not null, primary key
|
62
|
+
# user_id :integer
|
63
|
+
# context_id :integer
|
64
|
+
# context_type :string(255)
|
65
|
+
# role :string(255)
|
66
|
+
# created_at :datetime not null
|
67
|
+
# updated_at :datetime not null
|
68
|
+
#
|
69
|
+
|
@@ -0,0 +1,21 @@
|
|
1
|
+
class Subcontext < ActiveRecord::Base
|
2
|
+
belongs_to :context
|
3
|
+
has_many :permissions, :as => :context
|
4
|
+
|
5
|
+
alias_attribute :to_s, :title
|
6
|
+
|
7
|
+
def depth
|
8
|
+
context.depth + 1
|
9
|
+
end
|
10
|
+
end
|
11
|
+
# == Schema Information
|
12
|
+
#
|
13
|
+
# Table name: subcontexts
|
14
|
+
#
|
15
|
+
# id :integer not null, primary key
|
16
|
+
# title :string(255)
|
17
|
+
# context_id :integer
|
18
|
+
# created_at :datetime not null
|
19
|
+
# updated_at :datetime not null
|
20
|
+
#
|
21
|
+
|