tokak_engine 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.
data/.gitignore CHANGED
@@ -1 +1,3 @@
1
1
  pkg
2
+ doc
3
+ .yardoc
data/README.markdown ADDED
@@ -0,0 +1,9 @@
1
+ TokakEngine
2
+ ===========
3
+
4
+ Tokak Engine is the heart of Tokak services.
5
+
6
+ It consists of authentication system that uses passport.tokak.ru
7
+ for cross-service authentication and some common objects and modules.
8
+
9
+ Copyright © 2009 [Alexander Semyonov](http://rotuka.com/), released under the MIT license
data/Rakefile CHANGED
@@ -47,3 +47,5 @@ rescue LoadError
47
47
  abort "YARD is not available. In order to run yardoc, you must: sudo gem install yard"
48
48
  end
49
49
  end
50
+ task :rdoc => :yardoc
51
+ task :doc => :yardoc
data/VERSION CHANGED
@@ -1 +1 @@
1
- 0.0.2
1
+ 0.0.3
@@ -0,0 +1,11 @@
1
+ production: &production
2
+ canonical_host: service_name.tokak.ru
3
+ domain: .tokak.ru
4
+
5
+ development:
6
+ <<: *production
7
+ canonical_host: service_name.tokak.local
8
+ domain: .tokak.local
9
+
10
+ test:
11
+ <<: *production
@@ -1,3 +1,8 @@
1
+ # Rack middleware to redirect to canonical hosts from www and other ugly subdomain names
2
+ # @author tylerhunt, http://coderack.org/users/tylerhunt
3
+ # @example redirects to canonical host
4
+ # use CanonicalHost, 'example.com'
5
+ # @see http://coderack.org/users/tylerhunt/entries/6-canonical-host
1
6
  class CanonicalHost
2
7
  def initialize(app, host=nil, &block)
3
8
  @app = app
@@ -1,9 +1,50 @@
1
1
  module TokakEngine
2
2
  module Application
3
3
  extend self
4
+ mattr_accessor :config
4
5
 
5
- def load_config
6
- @@config = YAML.load_file(File.join(Rails.root, 'config', 'application.yml')).to_hash.with_indifferent_access[Rails.env]
6
+ # Default configuration options
7
+ def default_config
8
+ @@default_config = {
9
+ :canonical_host => 'tokak.ru',
10
+ :domain => '.tokak.ru'
11
+ }
12
+ end
13
+
14
+ # Autoload application config from application.yml
15
+ # @return [Hash] application configuration for current environment
16
+ def config
17
+ unless @@config
18
+ file_name = File.join(Rails.root, 'config', 'application.yml')
19
+ @@config = if File.exists?(file_name)
20
+ YAML.load_file(file_name).
21
+ to_hash.
22
+ with_indifferent_access[Rails.env]
23
+ else
24
+ {}
25
+ end.reverse_merge!(default_config)
26
+ end
27
+ @@config
28
+ end
29
+
30
+ # Retuns configured canonical host
31
+ def canonical_host
32
+ config[:canonical_host]
33
+ end
34
+
35
+ # Returns configured session domain
36
+ def domain
37
+ config[:domain]
38
+ end
39
+
40
+ def passport_url
41
+ "http://passport#{domain}/"
42
+ end
43
+
44
+ # Initialize canonical host and cookies (session) domain
45
+ def init_engine
46
+ ActionController::Base.session_options[:domain] = domain
47
+ ActionController::Dispatcher.middleware.use 'CanonicalHost', canonical_host
7
48
  end
8
49
  end
9
50
  end
@@ -16,57 +16,106 @@ module TokakEngine
16
16
  end
17
17
 
18
18
  module InstanceMethods
19
+ # Returns current user session
20
+ #
21
+ # @return [UserSession, nil] an Authlogic session object or nil if not signed in
19
22
  def current_user_session
20
23
  return @current_user_session if defined?(@current_user_session)
21
24
  @current_user_session = UserSession.find
22
25
  end
23
26
 
27
+ # Returns current user
28
+ #
29
+ # @return [User, nil] a signed in User or nil if anonymous user
24
30
  def current_user
25
31
  return @current_user if defined?(@current_user)
26
32
  @current_user = current_user_session && current_user_session.user
27
33
  end
28
34
 
35
+ # @deprecated Use {#current_user} instead
36
+ # Does user signed in?
37
+ #
38
+ # @return [User, nil]
29
39
  def logged_in?
30
40
  current_user
31
41
  end
32
42
 
43
+ # Filter to allow only signed in users
44
+ # @return [nil, false] return false to break filter chain and redirect to passport
33
45
  def require_user
34
- return deny_access unless logged_in?
46
+ return deny_access unless current_user
35
47
  end
36
48
 
49
+ # Filter to allow only anonymous users
50
+ # @return [nil, false] return false to break filter chain and redirect to account page
37
51
  def require_no_user
38
52
  return deny_access(:error,
39
53
  'You must be logged out to access this page',
40
54
  account_path) if current_user
41
55
  end
42
56
 
57
+ # Denies user access for page, set flash message
58
+ # @param [Symbol] status of flash message
59
+ # @param [String] default flash message (without I18n support)
60
+ # @param [String, Hash] path to redirect
43
61
  def deny_access(status = :error, message = 'You must be logged in to access this page', redirect = nil)
44
62
  store_location
45
63
  set_flash_message!(status, message)
46
64
  redirect_to(redirect || signin_path(:return_to => request.request_uri))
47
65
  return false
48
66
  end
67
+
68
+ # Path to passport signin
69
+ # @option options [String, Hash] :return_to Path to return to after signin in
70
+ def signin_path(options = {})
71
+ passport_path('signin', options)
72
+ end
73
+
74
+ # Path to passport signout
75
+ # @option options [String, Hash] :return_to Path to return to after signin out
76
+ def signout_path(options = {})
77
+ passport_path('signout', options)
78
+ end
49
79
 
50
80
  protected
81
+ def passport_path(relative_path, options = {})
82
+ returning('') do |url|
83
+ url << TokakEngine::Application.passport_url
84
+ url << relative_path
85
+ unless options.empty?
86
+ params = options.collect do |param, value|
87
+ "#{CGI.escape(param)}=#{CGI.escape(value)}"
88
+ end
89
+ url << "?#{params}"
90
+ end
91
+ end
92
+ end
93
+
94
+ # Stores location in session to come back after signing in/out
51
95
  def store_location
52
96
  if request.get?
53
- session[:return_to] = request.request_uri
97
+ cookies[:return_to] = request.request_uri
54
98
  end
55
99
  end
56
100
 
101
+ # Redirects back to stored location or default path
102
+ # @param [String, Hash] default path to redirect to
57
103
  def redirect_back_or(default)
58
104
  redirect_to(return_to || default)
59
105
  clear_return_to
60
106
  end
61
107
 
108
+ # Return path to redirect to after signin in/out
62
109
  def return_to
63
- session[:return_to] || params[:return_to]
110
+ cookies[:return_to] || params[:return_to]
64
111
  end
65
112
 
113
+ # Clears stored location for redirection
66
114
  def clear_return_to
67
- session[:return_to] = nil
115
+ cookies.delete(:return_to)
68
116
  end
69
117
 
118
+ # Redirects to root_path
70
119
  def redirect_to_root
71
120
  redirect_to(root_path)
72
121
  end
@@ -28,18 +28,25 @@ module TokakEngine
28
28
  module InstanceMethods
29
29
  unloadable
30
30
 
31
+ # Alias for {#avatars}
31
32
  def user_avatars(*args)
32
33
  self.avatars(*args)
33
34
  end
34
35
 
36
+ # Full user’s name
37
+ # @return [String, nil] name if first name or last one presents, nil if none presents
35
38
  def name
36
- [self.first_name, self.last_name].compact.join(' ')
39
+ [self.first_name, self.last_name].compact.join(' ') if self.first_name.present? || self.last_name.present?
37
40
  end
38
41
 
42
+ # Returns text representation of user
43
+ # @return [String] nick name or full name or email
39
44
  def to_s
40
45
  self.nick_name || self.name || self.email
41
46
  end
42
47
 
48
+ # User’s gender
49
+ # @return [String] user’s gender as I18zed string
43
50
  def gender
44
51
  I18n.t(GENDERS[self.gender_id], :prefix => 'options.user.gender', :default => GENDERS[self.gender_id].to_s.humanize)
45
52
  end
data/lib/tokak_engine.rb CHANGED
@@ -4,5 +4,5 @@ end
4
4
  require 'tokak_engine/authentication'
5
5
  require 'tokak_engine/application'
6
6
 
7
- TokakEngine::Application.load_config
7
+ TokakEngine::Application.config
8
8
  ActionView::Base.send(:include, TokakHelper)
data/rails/init.rb CHANGED
@@ -1 +1 @@
1
- # Include hook code here
1
+ require 'tokak­engine'
data/tokak_engine.gemspec CHANGED
@@ -5,19 +5,19 @@
5
5
 
6
6
  Gem::Specification.new do |s|
7
7
  s.name = %q{tokak_engine}
8
- s.version = "0.0.2"
8
+ s.version = "0.0.3"
9
9
 
10
10
  s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
11
11
  s.authors = ["Alexander Semyonov"]
12
- s.date = %q{2009-11-07}
12
+ s.date = %q{2009-11-08}
13
13
  s.email = %q{rotuka@tokak.ru}
14
14
  s.extra_rdoc_files = [
15
- "README"
15
+ "README.markdown"
16
16
  ]
17
17
  s.files = [
18
18
  ".gitignore",
19
19
  "MIT-LICENSE",
20
- "README",
20
+ "README.markdown",
21
21
  "Rakefile",
22
22
  "VERSION",
23
23
  "app/helpers/tokak_helper.rb",
@@ -29,6 +29,7 @@ Gem::Specification.new do |s|
29
29
  "app/views/user_avatars/_user_avatar.html.haml",
30
30
  "app/views/user_avatars/index.html.haml",
31
31
  "app/views/users/_user_bar.html.haml",
32
+ "config/application.yml",
32
33
  "db/migrate/20090926100710_create_users.rb",
33
34
  "db/migrate/20091017071436_create_identifiers.rb",
34
35
  "db/migrate/20091017071522_create_openid_identifiers.rb",
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: tokak_engine
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.2
4
+ version: 0.0.3
5
5
  platform: ruby
6
6
  authors:
7
7
  - Alexander Semyonov
@@ -9,7 +9,7 @@ autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
11
 
12
- date: 2009-11-07 00:00:00 +03:00
12
+ date: 2009-11-08 00:00:00 +03:00
13
13
  default_executable:
14
14
  dependencies: []
15
15
 
@@ -20,11 +20,11 @@ executables: []
20
20
  extensions: []
21
21
 
22
22
  extra_rdoc_files:
23
- - README
23
+ - README.markdown
24
24
  files:
25
25
  - .gitignore
26
26
  - MIT-LICENSE
27
- - README
27
+ - README.markdown
28
28
  - Rakefile
29
29
  - VERSION
30
30
  - app/helpers/tokak_helper.rb
@@ -36,6 +36,7 @@ files:
36
36
  - app/views/user_avatars/_user_avatar.html.haml
37
37
  - app/views/user_avatars/index.html.haml
38
38
  - app/views/users/_user_bar.html.haml
39
+ - config/application.yml
39
40
  - db/migrate/20090926100710_create_users.rb
40
41
  - db/migrate/20091017071436_create_identifiers.rb
41
42
  - db/migrate/20091017071522_create_openid_identifiers.rb
data/README DELETED
@@ -1,13 +0,0 @@
1
- TokakEngine
2
- ===========
3
-
4
- Introduction goes here.
5
-
6
-
7
- Example
8
- =======
9
-
10
- Example goes here.
11
-
12
-
13
- Copyright (c) 2009 [name of plugin creator], released under the MIT license