sso-auth 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,20 @@
1
+ Copyright 2012 YOURNAME
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.
@@ -0,0 +1,28 @@
1
+ = SsoAuth
2
+
3
+ == Usage
4
+
5
+ Gemfile
6
+
7
+ gem 'sso-auth'
8
+
9
+ Layout
10
+
11
+ <body>
12
+ <%= render :partial => "sso_auth/shared/header" %>
13
+ ...
14
+ <%= yield %>
15
+ ...
16
+ <%= render :partial => "sso_auth/shared/footer" %>
17
+ </body>
18
+
19
+ Stylesheet
20
+
21
+ *= require ...
22
+ *= require sso_auth/shared // common styles
23
+ *= require custom_sso_auth // customize styles
24
+ */
25
+
26
+ == License
27
+
28
+ This project rocks and uses MIT-LICENSE.
@@ -0,0 +1,33 @@
1
+ #!/usr/bin/env rake
2
+ begin
3
+ require 'bundler/setup'
4
+ rescue LoadError
5
+ puts 'You must `gem install bundler` and `bundle install` to run rake tasks'
6
+ end
7
+ begin
8
+ require 'rdoc/task'
9
+ rescue LoadError
10
+ require 'rdoc/rdoc'
11
+ require 'rake/rdoctask'
12
+ RDoc::Task = Rake::RDocTask
13
+ end
14
+
15
+ RDoc::Task.new(:rdoc) do |rdoc|
16
+ rdoc.rdoc_dir = 'rdoc'
17
+ rdoc.title = 'SsoAuth'
18
+ rdoc.options << '--line-numbers'
19
+ rdoc.rdoc_files.include('README.rdoc')
20
+ rdoc.rdoc_files.include('lib/**/*.rb')
21
+ end
22
+
23
+ APP_RAKEFILE = File.expand_path("../spec/dummy/Rakefile", __FILE__)
24
+ load 'rails/tasks/engine.rake'
25
+
26
+
27
+ Bundler::GemHelper.install_tasks
28
+
29
+ require 'rspec/core/rake_task'
30
+
31
+ RSpec::Core::RakeTask.new(:spec)
32
+
33
+ task :default => :spec
@@ -0,0 +1,24 @@
1
+ # encoding: utf-8
2
+
3
+ class SsoAuth::OmniauthCallbacksController < Devise::OmniauthCallbacksController
4
+ def identity
5
+ user = User.find_or_initialize_by_uid(request.env['omniauth.auth']['uid']).tap do |user|
6
+ attributes = request.env['omniauth.auth']['extra']['raw_info']['user']
7
+ attributes = attributes.merge(request.env['omniauth.auth']['info'])
8
+ attributes.each do |attribute, value|
9
+ user.send("#{attribute}=", value) if user.respond_to?("#{attribute}=")
10
+ end
11
+ user.save(:validate => false)
12
+ end
13
+
14
+ flash[:notice] = I18n.t "devise.omniauth_callbacks.success", :kind => "системы аутентификации"
15
+ sign_in user, :event => :authentication
16
+ redirect_to stored_location_for(:user) || main_app.root_path
17
+ end
18
+
19
+ private
20
+
21
+ def after_omniauth_failure_path_for(model)
22
+ main_app.root_path
23
+ end
24
+ end
@@ -0,0 +1,16 @@
1
+ class SsoAuth::SessionsController < ApplicationController
2
+ def destroy
3
+ reset_session
4
+ redirect_to "#{Settings['sso.url']}/users/sign_out?redirect_uri=#{CGI.escape(redirect_uri)}"
5
+ end
6
+
7
+ protected
8
+
9
+ def redirect_uri
10
+ URI.parse(request.url).tap do | uri |
11
+ uri.path = main_app.root_path
12
+ uri.query = nil
13
+ end.to_s
14
+ end
15
+
16
+ end
@@ -0,0 +1,11 @@
1
+ require 'open-uri'
2
+
3
+ class SsoAuth::UsersController < ApplicationController
4
+ respond_to :json
5
+
6
+ def index
7
+ authorize! :manage, :permissions
8
+ response = open("#{Settings['sso.url']}/users.json?user_search[keywords]=#{URI.escape(params[:term])}")
9
+ render :json => JSON.parse(response.read)
10
+ end
11
+ end
@@ -0,0 +1,6 @@
1
+ <% if current_user %>
2
+ <span class='current_user'><%= current_user.sso_auth_name %></span>
3
+ <%= link_to('Выход', sso_auth.destroy_user_session_path) %>
4
+ <% else %>
5
+ <%= link_to 'Вход в систему', user_omniauth_authorize_path(:identity) %>
6
+ <% end %>
@@ -0,0 +1,216 @@
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
+ # ==> ORM configuration
13
+ # Load and configure the ORM. Supports :active_record (default) and
14
+ # :mongoid (bson_ext recommended) by default. Other ORMs may be
15
+ # available as additional gems.
16
+ require 'devise/orm/active_record'
17
+
18
+ # ==> Configuration for any authentication mechanism
19
+ # Configure which keys are used when authenticating a user. The default is
20
+ # just :email. You can configure it to use [:username, :subdomain], so for
21
+ # authenticating a user, both parameters are required. Remember that those
22
+ # parameters are used only when authenticating and not when retrieving from
23
+ # session. If you need permissions, you should implement that in a before filter.
24
+ # You can also supply a hash where the value is a boolean determining whether
25
+ # or not authentication should be aborted when the value is not present.
26
+ # config.authentication_keys = [ :email ]
27
+
28
+ # Configure parameters from the request object used for authentication. Each entry
29
+ # given should be a request method and it will automatically be passed to the
30
+ # find_for_authentication method and considered in your model lookup. For instance,
31
+ # if you set :request_keys to [:subdomain], :subdomain will be used on authentication.
32
+ # The same considerations mentioned for authentication_keys also apply to request_keys.
33
+ # config.request_keys = []
34
+
35
+ # Configure which authentication keys should be case-insensitive.
36
+ # These keys will be downcased upon creating or modifying a user and when used
37
+ # to authenticate or find a user. Default is :email.
38
+ config.case_insensitive_keys = []
39
+
40
+ # Configure which authentication keys should have whitespace stripped.
41
+ # These keys will have whitespace before and after removed upon creating or
42
+ # modifying a user and when used to authenticate or find a user. Default is :email.
43
+ config.strip_whitespace_keys = []
44
+
45
+ # Tell if authentication through request.params is enabled. True by default.
46
+ # It can be set to an array that will enable params authentication only for the
47
+ # given strategies, for example, `config.params_authenticatable = [:database]` will
48
+ # enable it only for database (email + password) authentication.
49
+ # config.params_authenticatable = true
50
+
51
+ # Tell if authentication through HTTP Basic Auth is enabled. False by default.
52
+ # It can be set to an array that will enable http authentication only for the
53
+ # given strategies, for example, `config.http_authenticatable = [:token]` will
54
+ # enable it only for token authentication.
55
+ # config.http_authenticatable = false
56
+
57
+ # If http headers should be returned for AJAX requests. True by default.
58
+ # config.http_authenticatable_on_xhr = true
59
+
60
+ # The realm used in Http Basic Authentication. "Application" by default.
61
+ # config.http_authentication_realm = "Application"
62
+
63
+ # It will change confirmation, password recovery and other workflows
64
+ # to behave the same regardless if the e-mail provided was right or wrong.
65
+ # Does not affect registerable.
66
+ # config.paranoid = true
67
+
68
+ # By default Devise will store the user in session. You can skip storage for
69
+ # :http_auth and :token_auth by adding those symbols to the array below.
70
+ # Notice that if you are skipping storage for all authentication paths, you
71
+ # may want to disable generating routes to Devise's sessions controller by
72
+ # passing :skip => :sessions to `devise_for` in your config/routes.rb
73
+ config.skip_session_storage = [:http_auth]
74
+
75
+ # ==> Configuration for :database_authenticatable
76
+ # For bcrypt, this is the cost for hashing the password and defaults to 10. If
77
+ # using other encryptors, it sets how many times you want the password re-encrypted.
78
+ #
79
+ # Limiting the stretches to just one in testing will increase the performance of
80
+ # your test suite dramatically. However, it is STRONGLY RECOMMENDED to not use
81
+ # a value less than 10 in other environments.
82
+ config.stretches = Rails.env.test? ? 1 : 10
83
+
84
+ # Setup a pepper to generate the encrypted password.
85
+ # config.pepper = "d38daf1ff1526d1a6df451bc2a0cea2e2eb02dafa36d59b4852177ad91e8e3e5948a04c6f68302740e2187fc75cb84c5cf8a1aceaef6b9278df7b407546ddea1"
86
+
87
+ # ==> Configuration for :confirmable
88
+ # A period that the user is allowed to access the website even without
89
+ # confirming his account. For instance, if set to 2.days, the user will be
90
+ # able to access the website for two days without confirming his account,
91
+ # access will be blocked just in the third day. Default is 0.days, meaning
92
+ # the user cannot access the website without confirming his account.
93
+ # config.allow_unconfirmed_access_for = 2.days
94
+
95
+ # If true, requires any email changes to be confirmed (exactly the same way as
96
+ # initial account confirmation) to be applied. Requires additional unconfirmed_email
97
+ # db field (see migrations). Until confirmed new email is stored in
98
+ # unconfirmed email column, and copied to email column on successful confirmation.
99
+ config.reconfirmable = true
100
+
101
+ # Defines which key will be used when confirming an account
102
+ # config.confirmation_keys = [ :email ]
103
+
104
+ # ==> Configuration for :rememberable
105
+ # The time the user will be remembered without asking for credentials again.
106
+ # config.remember_for = 2.weeks
107
+
108
+ # If true, extends the user's remember period when remembered via cookie.
109
+ # config.extend_remember_period = false
110
+
111
+ # Options to be passed to the created cookie. For instance, you can set
112
+ # :secure => true in order to force SSL only cookies.
113
+ # config.rememberable_options = {}
114
+
115
+ # ==> Configuration for :validatable
116
+ # Range for password length. Default is 6..128.
117
+ # config.password_length = 6..128
118
+
119
+ # Email regex used to validate email formats. It simply asserts that
120
+ # an one (and only one) @ exists in the given string. This is mainly
121
+ # to give user feedback and not to assert the e-mail validity.
122
+ # config.email_regexp = /\A[^@]+@[^@]+\z/
123
+
124
+ # ==> Configuration for :timeoutable
125
+ # The time you want to timeout the user session without activity. After this
126
+ # time the user will be asked for credentials again. Default is 30 minutes.
127
+ # config.timeout_in = 30.minutes
128
+
129
+ # ==> Configuration for :lockable
130
+ # Defines which strategy will be used to lock an account.
131
+ # :failed_attempts = Locks an account after a number of failed attempts to sign in.
132
+ # :none = No lock strategy. You should handle locking by yourself.
133
+ # config.lock_strategy = :failed_attempts
134
+
135
+ # Defines which key will be used when locking and unlocking an account
136
+ # config.unlock_keys = [ :email ]
137
+
138
+ # Defines which strategy will be used to unlock an account.
139
+ # :email = Sends an unlock link to the user email
140
+ # :time = Re-enables login after a certain amount of time (see :unlock_in below)
141
+ # :both = Enables both strategies
142
+ # :none = No unlock strategy. You should handle unlocking by yourself.
143
+ # config.unlock_strategy = :both
144
+
145
+ # Number of authentication tries before locking an account if lock_strategy
146
+ # is failed attempts.
147
+ # config.maximum_attempts = 20
148
+
149
+ # Time interval to unlock the account if :time is enabled as unlock_strategy.
150
+ # config.unlock_in = 1.hour
151
+
152
+ # ==> Configuration for :recoverable
153
+ #
154
+ # Defines which key will be used when recovering the password for an account
155
+ # config.reset_password_keys = [ :email ]
156
+
157
+ # Time interval you can reset your password with a reset password key.
158
+ # Don't put a too small interval or your users won't have the time to
159
+ # change their passwords.
160
+ config.reset_password_within = 6.hours
161
+
162
+ # ==> Configuration for :encryptable
163
+ # Allow you to use another encryption algorithm besides bcrypt (default). You can use
164
+ # :sha1, :sha512 or encryptors from others authentication tools as :clearance_sha1,
165
+ # :authlogic_sha512 (then you should set stretches above to 20 for default behavior)
166
+ # and :restful_authentication_sha1 (then you should set stretches to 10, and copy
167
+ # REST_AUTH_SITE_KEY to pepper)
168
+ # config.encryptor = :sha512
169
+
170
+ # ==> Configuration for :token_authenticatable
171
+ # Defines name of the authentication token params key
172
+ # config.token_authentication_key = :auth_token
173
+
174
+ # ==> Scopes configuration
175
+ # Turn scoped views on. Before rendering "sessions/new", it will first check for
176
+ # "users/sessions/new". It's turned off by default because it's slower if you
177
+ # are using only default views.
178
+ # config.scoped_views = false
179
+
180
+ # Configure the default scope given to Warden. By default it's the first
181
+ # devise role declared in your routes (usually :user).
182
+ # config.default_scope = :user
183
+
184
+ # Configure sign_out behavior.
185
+ # Sign_out action can be scoped (i.e. /users/sign_out affects only :user scope).
186
+ # The default is true, which means any logout action will sign out all active scopes.
187
+ # config.sign_out_all_scopes = true
188
+
189
+ # ==> Navigation configuration
190
+ # Lists the formats that should be treated as navigational. Formats like
191
+ # :html, should redirect to the sign in page when the user does not have
192
+ # access, but formats like :xml or :json, should return 401.
193
+ #
194
+ # If you have any extra navigational formats, like :iphone or :mobile, you
195
+ # should add them to the navigational formats lists.
196
+ #
197
+ # The "*/*" below is required to match Internet Explorer requests.
198
+ # config.navigational_formats = ["*/*", :html]
199
+
200
+ # The default HTTP method used to sign out a resource. Default is :delete.
201
+ config.sign_out_via = :delete
202
+
203
+ # ==> OmniAuth
204
+ # Add a new OmniAuth provider. Check the wiki for more information on setting
205
+ # up on your models and hooks.
206
+ # config.omniauth :github, 'APP_ID', 'APP_SECRET', :scope => 'user,public_repo'
207
+
208
+ # ==> Warden configuration
209
+ # If you want to use other strategies, that are not supported by Devise, or
210
+ # change the failure app, you can configure them inside the config.warden block.
211
+ #
212
+ # config.warden do |manager|
213
+ # manager.intercept_401 = false
214
+ # manager.default_strategies(:scope => :user).unshift :some_external_strategy
215
+ # end
216
+ end
@@ -0,0 +1,11 @@
1
+ ru:
2
+ activerecord:
3
+ attributes:
4
+ permission:
5
+ context: Context
6
+ role: Role
7
+ user: User
8
+
9
+ models:
10
+ permission: Permission
11
+
@@ -0,0 +1,11 @@
1
+ ru:
2
+ activerecord:
3
+ attributes:
4
+ permission:
5
+ context: Контекст
6
+ role: Роль
7
+ user: Пользователь
8
+
9
+ models:
10
+ permission: Право доступа
11
+
@@ -0,0 +1,17 @@
1
+ SsoAuth::Engine.routes.draw do
2
+ resources :users, :only => :index
3
+ get 'sign_out' => 'sessions#destroy', :as => :destroy_user_session
4
+ end
5
+
6
+ Rails.application.routes.draw do
7
+ devise_for :users, :path => 'auth',
8
+ :controllers => {:omniauth_callbacks => 'sso_auth/omniauth_callbacks'},
9
+ :skip => [:sessions]
10
+
11
+ devise_scope :users do
12
+ get 'sign_in' => redirect('/auth/auth/identity'), :as => :new_user_session
13
+ end
14
+
15
+ mount SsoAuth::Engine => '/auth'
16
+ end
17
+
@@ -0,0 +1,40 @@
1
+ require 'rails/generators/migration'
2
+
3
+ module SsoAuth
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/permission.rb'
19
+ end
20
+
21
+ def create_controllers
22
+ template 'app/controllers/manage/application_controller.rb'
23
+ end
24
+
25
+ def create_seeds
26
+ template 'db/seeds.rb'
27
+ end
28
+
29
+ def create_specs
30
+ template 'spec/models/ability_spec.rb'
31
+ end
32
+
33
+ def create_migrations
34
+ migration_template 'db/migrate/create_users.rb'
35
+ migration_template 'db/migrate/create_permissions.rb'
36
+ end
37
+
38
+ end
39
+ end
40
+ end
@@ -0,0 +1,3 @@
1
+ class Manage::ApplicationController < ApplicationController
2
+ sso_load_and_authorize_resource
3
+ end
@@ -0,0 +1,17 @@
1
+ class Ability
2
+ include CanCan::Ability
3
+
4
+ def initialize(user)
5
+ return unless user
6
+
7
+ can :manage, :application do
8
+ user.permissions.any?
9
+ end
10
+
11
+ can :manage, :permissions do
12
+ user.manager?
13
+ end
14
+
15
+ # TODO: insert app specific rules here
16
+ end
17
+ end
@@ -0,0 +1,16 @@
1
+ # == Schema Information
2
+ #
3
+ # Table name: permissions
4
+ #
5
+ # id :integer not null, primary key
6
+ # user_id :integer
7
+ # context_id :integer
8
+ # context_type :string(255)
9
+ # role :string(255)
10
+ # created_at :datetime not null
11
+ # updated_at :datetime not null
12
+ #
13
+
14
+ class Permission < ActiveRecord::Base
15
+ sso_auth_permission :roles => [:manager, :operator]
16
+ end
@@ -0,0 +1,21 @@
1
+ # == Schema Information
2
+ #
3
+ # Table name: users
4
+ #
5
+ # id :integer not null, primary key
6
+ # uid :string(255)
7
+ # name :text
8
+ # email :text
9
+ # raw_info :text
10
+ # sign_in_count :integer
11
+ # current_sign_in_at :datetime
12
+ # last_sign_in_at :datetime
13
+ # current_sign_in_ip :string(255)
14
+ # last_sign_in_ip :string(255)
15
+ # created_at :datetime not null
16
+ # updated_at :datetime not null
17
+ #
18
+
19
+ class User < ActiveRecord::Base
20
+ sso_auth_user
21
+ end
@@ -0,0 +1,11 @@
1
+ class CreatePermissions < ActiveRecord::Migration
2
+ def change
3
+ create_table :permissions do |t|
4
+ t.references :user
5
+ t.references :context, :polymorphic => true
6
+ t.string :role
7
+ t.timestamps
8
+ end
9
+ add_index :permissions, [:user_id, :role, :context_id, :context_type], :name => 'by_user_and_role_and_context'
10
+ end
11
+ end
@@ -0,0 +1,20 @@
1
+ class CreateUsers < ActiveRecord::Migration
2
+ def change
3
+ create_table :users do | t |
4
+ t.string :uid # omniauth[:uid]
5
+ t.text :name, :email # omniauth[:info]
6
+ t.text :raw_info # omniauth[:extra]
7
+
8
+ # Devise trackable fields
9
+ t.integer :sign_in_count
10
+ t.datetime :current_sign_in_at
11
+ t.datetime :last_sign_in_at
12
+ t.string :current_sign_in_ip
13
+ t.string :last_sign_in_ip
14
+
15
+ t.timestamps
16
+ end
17
+
18
+ add_index :users, :uid
19
+ end
20
+ end
@@ -0,0 +1,4 @@
1
+ User.find_or_initialize_by_uid('1').tap do | user |
2
+ user.save(:validate => false)
3
+ user.permissions.create! :role => :manager if user.permissions.empty?
4
+ end
@@ -0,0 +1,14 @@
1
+ require 'spec_helper'
2
+
3
+ describe Ability do
4
+ context 'manager' do
5
+ subject { ability_for(manager) }
6
+ it { should be_able_to(:manage, :application) }
7
+ it { should be_able_to(:manage, :permissions) }
8
+ end
9
+ context 'operator' do
10
+ subject { ability_for(operator) }
11
+ it { should be_able_to(:manage, :application) }
12
+ it { should_not be_able_to(:manage, :permissions) }
13
+ end
14
+ end
@@ -0,0 +1,15 @@
1
+ require 'omniauth/strategies/oauth2'
2
+
3
+ module OmniAuth
4
+ module Strategies
5
+ class Identity < OmniAuth::Strategies::OAuth2
6
+ uid { raw_info['uid'] }
7
+ info { raw_info['info'] }
8
+ extra { {:raw_info => raw_info} }
9
+
10
+ def raw_info
11
+ @raw_info ||= access_token.get("/oauth/user.json?oauth_token=#{access_token.token}").parsed
12
+ end
13
+ end
14
+ end
15
+ end
@@ -0,0 +1,7 @@
1
+ require "sso-auth/engine"
2
+
3
+ require 'cancan'
4
+ require 'devise'
5
+
6
+ module SsoAuth
7
+ end
@@ -0,0 +1,79 @@
1
+ module SsoAuth
2
+ class Engine < ::Rails::Engine
3
+ isolate_namespace SsoAuth
4
+
5
+ config.after_initialize do
6
+ begin
7
+ Settings.define 'sso.url', :env_var => 'SSO_URL', :require => true
8
+ Settings.define 'sso.key', :env_var => 'SSO_KEY', :require => true
9
+ Settings.define 'sso.secret', :env_var => 'SSO_SECRET', :require => true
10
+
11
+ Settings.resolve!
12
+ rescue => e
13
+ puts "WARNING! #{e.message}"
14
+ end
15
+ end
16
+
17
+ initializer "sso_client.devise", :before => 'devise.omniauth' do |app|
18
+ require File.expand_path("../../../lib/omniauth/strategies/identity", __FILE__)
19
+ Devise.setup do |config|
20
+ config.omniauth :identity, Settings['sso.key'], Settings['sso.secret'], :client_options => { :site => Settings['sso.url'] }
21
+ end
22
+ end
23
+
24
+ config.to_prepare do
25
+ ActionController::Base.class_eval do
26
+ define_singleton_method :sso_load_and_authorize_resource do
27
+ before_filter :authenticate_user!
28
+ before_filter :authorize_user_can_manage_application!
29
+ load_and_authorize_resource
30
+ rescue_from CanCan::AccessDenied do |exception|
31
+ render :file => "#{Rails.root}/public/403", :formats => [:html], :status => 403, :layout => false
32
+ end
33
+ end
34
+
35
+ protected
36
+
37
+ define_method :authorize_user_can_manage_application! do
38
+ authorize! :manage, :application
39
+ end
40
+ end
41
+ ActiveRecord::Base.class_eval do
42
+ def self.sso_auth_user
43
+ has_many :permissions, :dependent => :destroy
44
+
45
+ devise :omniauthable, :trackable, :timeoutable
46
+
47
+ Permission.available_roles.each do |role|
48
+ define_method "#{role}_of?" do |context|
49
+ permissions.for_role(role).for_context(context).exists?
50
+ end
51
+ define_method "#{role}?" do
52
+ permissions.for_role(role).exists?
53
+ end
54
+ end
55
+
56
+ define_method :sso_auth_name do
57
+ email? ? "#{name} <#{email}>" : name
58
+ end
59
+ end
60
+
61
+ def self.sso_auth_permission(options)
62
+ define_singleton_method :available_roles do
63
+ options[:roles].map(&:to_s)
64
+ end
65
+
66
+ belongs_to :context, :polymorphic => true
67
+ belongs_to :user
68
+
69
+ validates_inclusion_of :role, :in => available_roles
70
+ validates_presence_of :role, :user
71
+ validates_uniqueness_of :role, :scope => [:user_id, :context_id, :context_type]
72
+
73
+ scope :for_role, ->(role) { where(:role => role) }
74
+ scope :for_context, ->(context) { where(:context_id => context.try(:id), :context_type => context.try(:class)) }
75
+ end
76
+ end
77
+ end
78
+ end
79
+ end
@@ -0,0 +1,45 @@
1
+ module SsoAuth
2
+ module SpecHelper
3
+
4
+ def ability_for(user)
5
+ Ability.new(user)
6
+ end
7
+
8
+ def create_user
9
+ @sequence ||= 0
10
+ @sequence += 1
11
+ User.create! :uid => @sequence, :name => "user #{@sequence}"
12
+ end
13
+
14
+ def user_with_role(role, context=nil, prefix=nil)
15
+ @roles ||= {}
16
+ @roles["#{prefix}_#{role}"] ||= {}
17
+ @roles["#{prefix}_#{role}"][context] ||= create_user.tap do |user|
18
+ user.permissions.create! :context => context, :role => role
19
+ end
20
+ end
21
+
22
+ def user
23
+ @user ||= create_user
24
+ end
25
+
26
+ def another_user
27
+ @another_user ||= create_user
28
+ end
29
+
30
+ Permission.available_roles.each do | role |
31
+ define_method "#{role}_of" do |context|
32
+ user_with_role role, context
33
+ end
34
+ define_method "#{role}" do
35
+ self.send("#{role}_of", nil)
36
+ end
37
+ define_method "another_#{role}_of" do |context|
38
+ user_with_role role, context, "another"
39
+ end
40
+ define_method "another_#{role}" do
41
+ self.send("another_#{role}_of", nil)
42
+ end
43
+ end
44
+ end
45
+ end
@@ -0,0 +1,3 @@
1
+ module SsoAuth
2
+ VERSION = "0.0.1"
3
+ end
metadata ADDED
@@ -0,0 +1,236 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: sso-auth
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ prerelease:
6
+ platform: ruby
7
+ authors:
8
+ - http://openteam.ru
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2012-11-16 00:00:00.000000000 Z
13
+ dependencies:
14
+ - !ruby/object:Gem::Dependency
15
+ name: cancan
16
+ requirement: !ruby/object:Gem::Requirement
17
+ none: false
18
+ requirements:
19
+ - - ! '>='
20
+ - !ruby/object:Gem::Version
21
+ version: '0'
22
+ type: :runtime
23
+ prerelease: false
24
+ version_requirements: !ruby/object:Gem::Requirement
25
+ none: false
26
+ requirements:
27
+ - - ! '>='
28
+ - !ruby/object:Gem::Version
29
+ version: '0'
30
+ - !ruby/object:Gem::Dependency
31
+ name: configliere
32
+ requirement: !ruby/object:Gem::Requirement
33
+ none: false
34
+ requirements:
35
+ - - ! '>='
36
+ - !ruby/object:Gem::Version
37
+ version: '0'
38
+ type: :runtime
39
+ prerelease: false
40
+ version_requirements: !ruby/object:Gem::Requirement
41
+ none: false
42
+ requirements:
43
+ - - ! '>='
44
+ - !ruby/object:Gem::Version
45
+ version: '0'
46
+ - !ruby/object:Gem::Dependency
47
+ name: devise
48
+ requirement: !ruby/object:Gem::Requirement
49
+ none: false
50
+ requirements:
51
+ - - ! '>='
52
+ - !ruby/object:Gem::Version
53
+ version: '0'
54
+ type: :runtime
55
+ prerelease: false
56
+ version_requirements: !ruby/object:Gem::Requirement
57
+ none: false
58
+ requirements:
59
+ - - ! '>='
60
+ - !ruby/object:Gem::Version
61
+ version: '0'
62
+ - !ruby/object:Gem::Dependency
63
+ name: omniauth
64
+ requirement: !ruby/object:Gem::Requirement
65
+ none: false
66
+ requirements:
67
+ - - ! '>='
68
+ - !ruby/object:Gem::Version
69
+ version: '0'
70
+ type: :runtime
71
+ prerelease: false
72
+ version_requirements: !ruby/object:Gem::Requirement
73
+ none: false
74
+ requirements:
75
+ - - ! '>='
76
+ - !ruby/object:Gem::Version
77
+ version: '0'
78
+ - !ruby/object:Gem::Dependency
79
+ name: omniauth-oauth2
80
+ requirement: !ruby/object:Gem::Requirement
81
+ none: false
82
+ requirements:
83
+ - - ! '>='
84
+ - !ruby/object:Gem::Version
85
+ version: '0'
86
+ type: :runtime
87
+ prerelease: false
88
+ version_requirements: !ruby/object:Gem::Requirement
89
+ none: false
90
+ requirements:
91
+ - - ! '>='
92
+ - !ruby/object:Gem::Version
93
+ version: '0'
94
+ - !ruby/object:Gem::Dependency
95
+ name: annotate
96
+ requirement: !ruby/object:Gem::Requirement
97
+ none: false
98
+ requirements:
99
+ - - ! '>='
100
+ - !ruby/object:Gem::Version
101
+ version: '0'
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: '0'
110
+ - !ruby/object:Gem::Dependency
111
+ name: rails
112
+ requirement: !ruby/object:Gem::Requirement
113
+ none: false
114
+ requirements:
115
+ - - ! '>='
116
+ - !ruby/object:Gem::Version
117
+ version: '0'
118
+ type: :development
119
+ prerelease: false
120
+ version_requirements: !ruby/object:Gem::Requirement
121
+ none: false
122
+ requirements:
123
+ - - ! '>='
124
+ - !ruby/object:Gem::Version
125
+ version: '0'
126
+ - !ruby/object:Gem::Dependency
127
+ name: rspec-rails
128
+ requirement: !ruby/object:Gem::Requirement
129
+ none: false
130
+ requirements:
131
+ - - ! '>='
132
+ - !ruby/object:Gem::Version
133
+ version: '0'
134
+ type: :development
135
+ prerelease: false
136
+ version_requirements: !ruby/object:Gem::Requirement
137
+ none: false
138
+ requirements:
139
+ - - ! '>='
140
+ - !ruby/object:Gem::Version
141
+ version: '0'
142
+ - !ruby/object:Gem::Dependency
143
+ name: shoulda-matchers
144
+ requirement: !ruby/object:Gem::Requirement
145
+ none: false
146
+ requirements:
147
+ - - ! '>='
148
+ - !ruby/object:Gem::Version
149
+ version: '0'
150
+ type: :development
151
+ prerelease: false
152
+ version_requirements: !ruby/object:Gem::Requirement
153
+ none: false
154
+ requirements:
155
+ - - ! '>='
156
+ - !ruby/object:Gem::Version
157
+ version: '0'
158
+ - !ruby/object:Gem::Dependency
159
+ name: sqlite3
160
+ requirement: !ruby/object:Gem::Requirement
161
+ none: false
162
+ requirements:
163
+ - - ! '>='
164
+ - !ruby/object:Gem::Version
165
+ version: '0'
166
+ type: :development
167
+ prerelease: false
168
+ version_requirements: !ruby/object:Gem::Requirement
169
+ none: false
170
+ requirements:
171
+ - - ! '>='
172
+ - !ruby/object:Gem::Version
173
+ version: '0'
174
+ description: Description of SsoAuth.
175
+ email:
176
+ - mail@openteam.ru
177
+ executables: []
178
+ extensions: []
179
+ extra_rdoc_files: []
180
+ files:
181
+ - app/controllers/sso_auth/omniauth_callbacks_controller.rb
182
+ - app/controllers/sso_auth/sessions_controller.rb
183
+ - app/controllers/sso_auth/users_controller.rb
184
+ - app/views/sso-auth/shared/_user_box.html.erb
185
+ - config/initializers/devise.rb
186
+ - config/locales/en.yml
187
+ - config/locales/ru.yml
188
+ - config/routes.rb
189
+ - lib/generators/sso-auth/install/install_generator.rb
190
+ - lib/generators/sso-auth/install/templates/app/controllers/manage/application_controller.rb
191
+ - lib/generators/sso-auth/install/templates/app/models/ability.rb
192
+ - lib/generators/sso-auth/install/templates/app/models/permission.rb
193
+ - lib/generators/sso-auth/install/templates/app/models/user.rb
194
+ - lib/generators/sso-auth/install/templates/db/migrate/create_permissions.rb
195
+ - lib/generators/sso-auth/install/templates/db/migrate/create_users.rb
196
+ - lib/generators/sso-auth/install/templates/db/seeds.rb
197
+ - lib/generators/sso-auth/install/templates/spec/models/ability_spec.rb
198
+ - lib/omniauth/strategies/identity.rb
199
+ - lib/sso-auth/engine.rb
200
+ - lib/sso-auth/spec_helper.rb
201
+ - lib/sso-auth/version.rb
202
+ - lib/sso-auth.rb
203
+ - MIT-LICENSE
204
+ - Rakefile
205
+ - README.rdoc
206
+ homepage:
207
+ licenses: []
208
+ post_install_message:
209
+ rdoc_options: []
210
+ require_paths:
211
+ - lib
212
+ required_ruby_version: !ruby/object:Gem::Requirement
213
+ none: false
214
+ requirements:
215
+ - - ! '>='
216
+ - !ruby/object:Gem::Version
217
+ version: '0'
218
+ segments:
219
+ - 0
220
+ hash: 4589756721612764360
221
+ required_rubygems_version: !ruby/object:Gem::Requirement
222
+ none: false
223
+ requirements:
224
+ - - ! '>='
225
+ - !ruby/object:Gem::Version
226
+ version: '0'
227
+ segments:
228
+ - 0
229
+ hash: 4589756721612764360
230
+ requirements: []
231
+ rubyforge_project:
232
+ rubygems_version: 1.8.24
233
+ signing_key:
234
+ specification_version: 3
235
+ summary: Summary of SsoAuth.
236
+ test_files: []