auth_client 0.0.2 → 0.0.3
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 +4 -4
- data/README.md +24 -18
- data/auth_client.gemspec +3 -1
- data/lib/auth_client.rb +3 -6
- data/lib/auth_client/helpers.rb +61 -0
- data/lib/auth_client/permission.rb +32 -0
- data/lib/auth_client/subscriber.rb +47 -0
- data/lib/auth_client/user.rb +90 -13
- data/lib/auth_client/version.rb +1 -1
- data/lib/generators/auth_client/install_generator.rb +17 -0
- data/lib/generators/auth_client/templates/permission.rb +5 -0
- data/lib/generators/auth_client/templates/user.rb +7 -0
- metadata +38 -5
- data/lib/auth_client/auth_client_helpers.rb +0 -51
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 0ee62d1441bd1818134640c3133387764de22742
|
4
|
+
data.tar.gz: fee00f50739000a0729390831459529cc588f652
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 652ac1275888cf944657c03fa5b87ff98174e01de480a99fa9dcf4c09812a854f74254849568ea11355ecac719641b0ce72053ea1d1feaa67d36db173eeedf22
|
7
|
+
data.tar.gz: 8db6b06baf79406eb8782003318b1f2f9c4589e016d662a037395b2866474b9b1eb2dc06cf60b2ca90f9f5be3a7fd823fba6e51aa6393b8571c58f4bd58bad03
|
data/README.md
CHANGED
@@ -1,7 +1,5 @@
|
|
1
1
|
# AuthClient
|
2
2
|
|
3
|
-
TODO: Write a gem description
|
4
|
-
|
5
3
|
## Installation
|
6
4
|
|
7
5
|
Add this line to your application's Gemfile:
|
@@ -11,19 +9,27 @@ Add this line to your application's Gemfile:
|
|
11
9
|
And then execute:
|
12
10
|
|
13
11
|
$ bundle
|
14
|
-
|
15
|
-
|
16
|
-
|
17
|
-
|
18
|
-
|
19
|
-
|
20
|
-
|
21
|
-
|
22
|
-
|
23
|
-
|
24
|
-
|
25
|
-
|
26
|
-
|
27
|
-
|
28
|
-
|
29
|
-
|
12
|
+
|
13
|
+
Run generator:
|
14
|
+
|
15
|
+
bundle exec rails generate auth_client:install
|
16
|
+
|
17
|
+
Or use similar models
|
18
|
+
|
19
|
+
```ruby
|
20
|
+
class User
|
21
|
+
include AuthClient::User
|
22
|
+
|
23
|
+
# your code goes here
|
24
|
+
end
|
25
|
+
```
|
26
|
+
|
27
|
+
```ruby
|
28
|
+
class Permission < ActiveRecord::Base
|
29
|
+
include AuthClient::Permission
|
30
|
+
|
31
|
+
acts_as_auth_client_permission roles: [:admin]
|
32
|
+
|
33
|
+
# your code goes here
|
34
|
+
end
|
35
|
+
```
|
data/auth_client.gemspec
CHANGED
@@ -21,7 +21,9 @@ Gem::Specification.new do |spec|
|
|
21
21
|
spec.add_development_dependency "bundler", "~> 1.6"
|
22
22
|
spec.add_development_dependency "rake"
|
23
23
|
|
24
|
-
spec.add_dependency 'auth_redis_user_connector'
|
25
24
|
spec.add_dependency 'activesupport'
|
25
|
+
spec.add_dependency 'auth_redis_user_connector'
|
26
26
|
spec.add_dependency 'configliere'
|
27
|
+
spec.add_dependency 'daemons'
|
28
|
+
spec.add_dependency 'rails'
|
27
29
|
end
|
data/lib/auth_client.rb
CHANGED
@@ -1,13 +1,10 @@
|
|
1
1
|
require "auth_client/version"
|
2
2
|
|
3
|
+
require 'auth_client/helpers'
|
4
|
+
require 'auth_client/permission'
|
3
5
|
require 'auth_client/user'
|
4
|
-
require 'auth_client/
|
6
|
+
require 'auth_client/subscriber' if defined?(Rails)
|
5
7
|
|
6
8
|
module AuthClient
|
7
9
|
end
|
8
10
|
|
9
|
-
ActiveSupport.on_load :action_controller do
|
10
|
-
include AuthClientHelpers
|
11
|
-
|
12
|
-
helper_method :current_user, :user_signed_in?, :sign_in_url, :sign_out_url
|
13
|
-
end
|
@@ -0,0 +1,61 @@
|
|
1
|
+
require 'active_support/concern'
|
2
|
+
require 'configliere'
|
3
|
+
|
4
|
+
module AuthClient
|
5
|
+
module Helpers
|
6
|
+
extend ActiveSupport::Concern
|
7
|
+
|
8
|
+
included do
|
9
|
+
before_action :check_session
|
10
|
+
end
|
11
|
+
|
12
|
+
def current_user
|
13
|
+
@current_user ||= ::User.find_by(id: session_user_id)
|
14
|
+
end
|
15
|
+
|
16
|
+
def user_signed_in?
|
17
|
+
!!current_user
|
18
|
+
end
|
19
|
+
|
20
|
+
def sign_in_url
|
21
|
+
uri = URI.parse(Settings['auth_server.sign_in_url'])
|
22
|
+
|
23
|
+
uri.query = { :redirect_url => request.original_url }.to_query
|
24
|
+
|
25
|
+
uri.to_s
|
26
|
+
end
|
27
|
+
|
28
|
+
def sign_out_url
|
29
|
+
uri = URI.parse(Settings['auth_server.sign_out_url'])
|
30
|
+
|
31
|
+
uri.query = { :redirect_url => request.original_url }.to_query
|
32
|
+
|
33
|
+
uri.to_s
|
34
|
+
end
|
35
|
+
|
36
|
+
private
|
37
|
+
|
38
|
+
def session_user_id
|
39
|
+
session['warden.user.user.key'].try(:first).try(:first)
|
40
|
+
end
|
41
|
+
|
42
|
+
def check_session
|
43
|
+
if session['warden.user.user.session']
|
44
|
+
last_request_at = session['warden.user.user.session']['last_request_at']
|
45
|
+
if Time.zone.now.to_i - last_request_at > 1800
|
46
|
+
session.clear
|
47
|
+
else
|
48
|
+
session['warden.user.user.session']['last_request_at'] = Time.zone.now.to_i
|
49
|
+
|
50
|
+
current_user.activity_notify if current_user
|
51
|
+
end
|
52
|
+
end
|
53
|
+
end
|
54
|
+
end
|
55
|
+
end
|
56
|
+
|
57
|
+
ActiveSupport.on_load :action_controller do
|
58
|
+
include AuthClient::Helpers
|
59
|
+
|
60
|
+
helper_method :current_user, :user_signed_in?, :sign_in_url, :sign_out_url
|
61
|
+
end
|
@@ -0,0 +1,32 @@
|
|
1
|
+
require 'active_support/concern'
|
2
|
+
|
3
|
+
module AuthClient
|
4
|
+
module Permission
|
5
|
+
extend ActiveSupport::Concern
|
6
|
+
|
7
|
+
def user
|
8
|
+
::User.find_by id: user_id
|
9
|
+
end
|
10
|
+
|
11
|
+
module ClassMethods
|
12
|
+
def acts_as_auth_client_permission(roles: roles)
|
13
|
+
define_singleton_method :available_roles do
|
14
|
+
roles.map(&:to_s)
|
15
|
+
end
|
16
|
+
|
17
|
+
delegate :info_notify, :to => :user, :prefix => true, :allow_nil => true
|
18
|
+
|
19
|
+
after_destroy :user_info_notify
|
20
|
+
after_save :user_info_notify
|
21
|
+
|
22
|
+
belongs_to :context, :polymorphic => true
|
23
|
+
|
24
|
+
scope :for_role, ->(role) { where(:role => role) }
|
25
|
+
scope :for_context, ->(context) { where(:context_id => context.try(:id), :context_type => context.try(:class)) }
|
26
|
+
|
27
|
+
validates_inclusion_of :role, :in => available_roles + available_roles.map(&:to_sym)
|
28
|
+
validates_presence_of :role
|
29
|
+
end
|
30
|
+
end
|
31
|
+
end
|
32
|
+
end
|
@@ -0,0 +1,47 @@
|
|
1
|
+
require 'auth_redis_user_connector'
|
2
|
+
require 'daemons'
|
3
|
+
|
4
|
+
module AuthClient
|
5
|
+
class Subscriber < ::Rails::Railtie
|
6
|
+
rake_tasks do
|
7
|
+
namespace :subscriber do
|
8
|
+
desc 'Start listen channel'
|
9
|
+
task :start => :environment do
|
10
|
+
Daemons.call(:app_name => 'subscriber', :multiple => false, :dir_mode => :normal, :dir => 'tmp/pids') do
|
11
|
+
logger = Logger.new("#{Rails.root}/log/subscriber.log")
|
12
|
+
|
13
|
+
begin
|
14
|
+
RedisUserConnector.sub('broadcast') do |on|
|
15
|
+
on.subscribe do
|
16
|
+
logger.info 'Subscribed to broadcast channel'
|
17
|
+
end
|
18
|
+
|
19
|
+
on.message do |_, message|
|
20
|
+
logger.info "Recieved message about user <#{message}> signed in"
|
21
|
+
::User.find_by(:id => message).try :after_signed_in
|
22
|
+
end
|
23
|
+
|
24
|
+
on.unsubscribe do
|
25
|
+
logger.info 'Unsubscribed from broadcast channel'
|
26
|
+
end
|
27
|
+
end
|
28
|
+
rescue Exception => e
|
29
|
+
logger.fatal e
|
30
|
+
end
|
31
|
+
end
|
32
|
+
end
|
33
|
+
|
34
|
+
desc 'Stop listen channel'
|
35
|
+
task :stop => :environment do
|
36
|
+
Daemons::Monitor.find('tmp/pids', 'subscriber').try :stop
|
37
|
+
end
|
38
|
+
|
39
|
+
desc 'Restart subscriber'
|
40
|
+
task :restart => :environment do
|
41
|
+
Rake::Task['subscriber:stop'].invoke
|
42
|
+
Rake::Task['subscriber:start'].invoke
|
43
|
+
end
|
44
|
+
end
|
45
|
+
end
|
46
|
+
end
|
47
|
+
end
|
data/lib/auth_client/user.rb
CHANGED
@@ -1,21 +1,98 @@
|
|
1
|
+
require 'active_support/concern'
|
1
2
|
require 'auth_redis_user_connector'
|
2
3
|
|
3
|
-
|
4
|
-
|
5
|
-
|
4
|
+
module AuthClient
|
5
|
+
module User
|
6
|
+
extend ActiveSupport::Concern
|
6
7
|
|
7
|
-
|
8
|
-
|
8
|
+
included do
|
9
|
+
acts_as_auth_client_user
|
10
|
+
end
|
9
11
|
|
10
|
-
|
11
|
-
|
12
|
-
|
13
|
-
|
12
|
+
def to_s
|
13
|
+
[surname, name, patronymic].compact.join(' ')
|
14
|
+
end
|
15
|
+
|
16
|
+
def fullname
|
17
|
+
to_s
|
18
|
+
end
|
19
|
+
|
20
|
+
def app_name
|
21
|
+
end
|
22
|
+
|
23
|
+
def check_app_name
|
24
|
+
raise 'User#app_name should not be blank' if app_name.blank?
|
14
25
|
end
|
15
|
-
end
|
16
26
|
|
17
|
-
|
18
|
-
|
27
|
+
def activity_notify
|
28
|
+
check_app_name
|
29
|
+
|
30
|
+
RedisUserConnector.set id, "#{app_name}_last_activity", Time.zone.now.to_i
|
31
|
+
end
|
32
|
+
|
33
|
+
def info_notify
|
34
|
+
check_app_name
|
35
|
+
|
36
|
+
RedisUserConnector.set id, "#{app_name}_info", info_hash.to_json
|
37
|
+
end
|
38
|
+
|
39
|
+
def info_hash
|
40
|
+
{ :permissions => permissions_info, :url => "https://#{app_name}.tusur.ru/" }
|
41
|
+
end
|
42
|
+
|
43
|
+
def permissions_info
|
44
|
+
permissions.map { |p| { :role => p.role, :info => p.context.try(:to_s) }}
|
45
|
+
end
|
46
|
+
|
47
|
+
def after_signed_in
|
48
|
+
info_notify
|
49
|
+
end
|
50
|
+
|
51
|
+
def last_activity_at
|
52
|
+
return nil if app_name.blank?
|
53
|
+
|
54
|
+
seconds = instance_variable_get("@#{app_name}_last_activity").to_i
|
55
|
+
|
56
|
+
Time.at(seconds)
|
57
|
+
end
|
58
|
+
|
59
|
+
module ClassMethods
|
60
|
+
def acts_as_auth_client_user
|
61
|
+
define_method :permissions do
|
62
|
+
::Permission.where :user_id => id
|
63
|
+
end
|
64
|
+
|
65
|
+
define_method(:has_permission?) do |role:, context: nil|
|
66
|
+
context ?
|
67
|
+
permissions.for_role(role).for_context(context).exists? :
|
68
|
+
permissions.for_role(role).exists?
|
69
|
+
end
|
70
|
+
end
|
71
|
+
|
72
|
+
def find_by(id:)
|
73
|
+
redis_data = RedisUserConnector.get(id)
|
74
|
+
|
75
|
+
return nil if (redis_data.nil? || redis_data.empty?)
|
76
|
+
|
77
|
+
attributes = redis_data.merge(:id => id)
|
78
|
+
|
79
|
+
build_user attributes
|
80
|
+
end
|
81
|
+
|
82
|
+
private
|
83
|
+
|
84
|
+
def build_user(attributes)
|
85
|
+
new.tap do |user|
|
86
|
+
attributes.each do |attribute, value|
|
87
|
+
name = "@#{attribute}"
|
88
|
+
user.instance_variable_set name, value
|
89
|
+
|
90
|
+
user.define_singleton_method attribute do
|
91
|
+
instance_variable_get name
|
92
|
+
end
|
93
|
+
end
|
94
|
+
end
|
95
|
+
end
|
96
|
+
end
|
19
97
|
end
|
20
98
|
end
|
21
|
-
|
data/lib/auth_client/version.rb
CHANGED
@@ -0,0 +1,17 @@
|
|
1
|
+
require 'rails/generators'
|
2
|
+
|
3
|
+
module AuthClient
|
4
|
+
class InstallGenerator < Rails::Generators::Base
|
5
|
+
source_root File.expand_path("../templates", __FILE__)
|
6
|
+
|
7
|
+
desc 'Setup AuthClient'
|
8
|
+
|
9
|
+
def copy_user
|
10
|
+
copy_file 'user.rb', 'app/models/user.rb'
|
11
|
+
end
|
12
|
+
|
13
|
+
def copy_permission
|
14
|
+
copy_file 'permission.rb', 'app/models/permission.rb'
|
15
|
+
end
|
16
|
+
end
|
17
|
+
end
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: auth_client
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.0.
|
4
|
+
version: 0.0.3
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- OpenTeam
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2014-09
|
11
|
+
date: 2014-10-09 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: bundler
|
@@ -39,7 +39,7 @@ dependencies:
|
|
39
39
|
- !ruby/object:Gem::Version
|
40
40
|
version: '0'
|
41
41
|
- !ruby/object:Gem::Dependency
|
42
|
-
name:
|
42
|
+
name: activesupport
|
43
43
|
requirement: !ruby/object:Gem::Requirement
|
44
44
|
requirements:
|
45
45
|
- - ">="
|
@@ -53,7 +53,7 @@ dependencies:
|
|
53
53
|
- !ruby/object:Gem::Version
|
54
54
|
version: '0'
|
55
55
|
- !ruby/object:Gem::Dependency
|
56
|
-
name:
|
56
|
+
name: auth_redis_user_connector
|
57
57
|
requirement: !ruby/object:Gem::Requirement
|
58
58
|
requirements:
|
59
59
|
- - ">="
|
@@ -80,6 +80,34 @@ dependencies:
|
|
80
80
|
- - ">="
|
81
81
|
- !ruby/object:Gem::Version
|
82
82
|
version: '0'
|
83
|
+
- !ruby/object:Gem::Dependency
|
84
|
+
name: daemons
|
85
|
+
requirement: !ruby/object:Gem::Requirement
|
86
|
+
requirements:
|
87
|
+
- - ">="
|
88
|
+
- !ruby/object:Gem::Version
|
89
|
+
version: '0'
|
90
|
+
type: :runtime
|
91
|
+
prerelease: false
|
92
|
+
version_requirements: !ruby/object:Gem::Requirement
|
93
|
+
requirements:
|
94
|
+
- - ">="
|
95
|
+
- !ruby/object:Gem::Version
|
96
|
+
version: '0'
|
97
|
+
- !ruby/object:Gem::Dependency
|
98
|
+
name: rails
|
99
|
+
requirement: !ruby/object:Gem::Requirement
|
100
|
+
requirements:
|
101
|
+
- - ">="
|
102
|
+
- !ruby/object:Gem::Version
|
103
|
+
version: '0'
|
104
|
+
type: :runtime
|
105
|
+
prerelease: false
|
106
|
+
version_requirements: !ruby/object:Gem::Requirement
|
107
|
+
requirements:
|
108
|
+
- - ">="
|
109
|
+
- !ruby/object:Gem::Version
|
110
|
+
version: '0'
|
83
111
|
description: Auth Client
|
84
112
|
email:
|
85
113
|
- developers@openteam.ru
|
@@ -94,9 +122,14 @@ files:
|
|
94
122
|
- Rakefile
|
95
123
|
- auth_client.gemspec
|
96
124
|
- lib/auth_client.rb
|
97
|
-
- lib/auth_client/
|
125
|
+
- lib/auth_client/helpers.rb
|
126
|
+
- lib/auth_client/permission.rb
|
127
|
+
- lib/auth_client/subscriber.rb
|
98
128
|
- lib/auth_client/user.rb
|
99
129
|
- lib/auth_client/version.rb
|
130
|
+
- lib/generators/auth_client/install_generator.rb
|
131
|
+
- lib/generators/auth_client/templates/permission.rb
|
132
|
+
- lib/generators/auth_client/templates/user.rb
|
100
133
|
homepage: https://github.com/openteam-com
|
101
134
|
licenses:
|
102
135
|
- MIT
|
@@ -1,51 +0,0 @@
|
|
1
|
-
require 'active_support/concern'
|
2
|
-
require 'configliere'
|
3
|
-
|
4
|
-
module AuthClientHelpers
|
5
|
-
extend ActiveSupport::Concern
|
6
|
-
|
7
|
-
included do
|
8
|
-
before_action :check_session
|
9
|
-
end
|
10
|
-
|
11
|
-
def current_user
|
12
|
-
@current_user ||= User.find_by(:id => session_user_id)
|
13
|
-
end
|
14
|
-
|
15
|
-
def user_signed_in?
|
16
|
-
!!current_user
|
17
|
-
end
|
18
|
-
|
19
|
-
def sign_in_url
|
20
|
-
uri = URI.parse(Settings['auth_server.sign_in_url'])
|
21
|
-
|
22
|
-
uri.query = { :redirect_url => request.original_url }.to_query
|
23
|
-
|
24
|
-
uri.to_s
|
25
|
-
end
|
26
|
-
|
27
|
-
def sign_out_url
|
28
|
-
uri = URI.parse(Settings['auth_server.sign_out_url'])
|
29
|
-
|
30
|
-
uri.query = { :redirect_url => request.original_url }.to_query
|
31
|
-
|
32
|
-
uri.to_s
|
33
|
-
end
|
34
|
-
|
35
|
-
private
|
36
|
-
|
37
|
-
def session_user_id
|
38
|
-
session['warden.user.user.key'].try(:first).try(:first)
|
39
|
-
end
|
40
|
-
|
41
|
-
def check_session
|
42
|
-
if session['warden.user.user.session']
|
43
|
-
last_request_at = session['warden.user.user.session']['last_request_at']
|
44
|
-
if Time.zone.now.to_i - last_request_at > 1800
|
45
|
-
session.clear
|
46
|
-
else
|
47
|
-
session['warden.user.user.session']['last_request_at'] = Time.zone.now.to_i
|
48
|
-
end
|
49
|
-
end
|
50
|
-
end
|
51
|
-
end
|