easy_login 0.1.3 → 1.0.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/README.md +48 -23
- data/lib/easy_login/config.rb +16 -10
- data/lib/easy_login/redirect.rb +22 -0
- data/lib/easy_login/session.rb +3 -3
- data/lib/easy_login/version.rb +1 -1
- data/lib/easy_login.rb +24 -0
- data/pkg/easy_login-0.1.3.gem +0 -0
- metadata +4 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 6b73671dcbbc9bdf09353704254e6042be5a61de
|
4
|
+
data.tar.gz: e35a8ba2407ab184d244cb74239c0e21005ae377
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: b4678c947c3773f08a50d893d055674d668a0647b3c5f4ac54c31ed83815508151dbd4a5da5c126887bba9e900e7545810b7623156ec443975f8d9ae53b3b098
|
7
|
+
data.tar.gz: 3108ef0353236cd01d3abc696b8a889c13f9323f1ef598ea13417199ef3fc0e8adfba1dd60bd08ca25dcb8cc1ddb74607e91481637eaa55e759bb21894c5c3d9
|
data/README.md
CHANGED
@@ -1,6 +1,7 @@
|
|
1
1
|
# EasyLogin
|
2
2
|
|
3
|
-
|
3
|
+
A simple session controller which just including following method for
|
4
|
+
controllers
|
4
5
|
|
5
6
|
+ :sign_in
|
6
7
|
+ :sign_out
|
@@ -8,48 +9,72 @@ a simple session controller which just including
|
|
8
9
|
+ :current_user
|
9
10
|
+ :current_user?
|
10
11
|
|
11
|
-
for
|
12
|
+
And forllowings for veiw helper
|
12
13
|
|
13
14
|
+ :current_user
|
14
15
|
+ :current_user?
|
15
16
|
+ :signed_in?
|
16
17
|
|
17
|
-
for views
|
18
|
-
|
19
18
|
## Installation
|
20
19
|
|
21
|
-
|
22
|
-
|
23
|
-
```ruby
|
24
|
-
gem 'easy_login'
|
25
|
-
```
|
26
|
-
|
27
|
-
And then execute:
|
28
|
-
|
29
|
-
$ bundle install
|
30
|
-
|
31
|
-
Or install it yourself as:
|
20
|
+
Install it yourself as:
|
32
21
|
|
33
22
|
$ gem install easy_login
|
34
23
|
|
35
24
|
## Usage
|
36
25
|
|
37
|
-
|
26
|
+
Added config in `config/application.rb` or `config/environments/*.rb`
|
38
27
|
|
39
28
|
```ruby
|
40
|
-
|
41
|
-
|
42
|
-
|
29
|
+
EasyLogin.setup do |config|
|
30
|
+
config.salt = "same_salt_string"
|
31
|
+
config.user_model = "your user model such as 'User'"
|
32
|
+
config.user_role_attr = "your role attribute in user model such as 'role'"
|
43
33
|
end
|
44
34
|
```
|
45
35
|
|
46
|
-
And then
|
36
|
+
And then abosultely use all methods above in controller and view
|
37
|
+
|
38
|
+
Also you can declare a redirect schema for differenct user accessing differect
|
39
|
+
controller and action
|
40
|
+
Add a config file to `config/redirect_to.yml` as following
|
41
|
+
|
42
|
+
```yaml
|
43
|
+
default:
|
44
|
+
unsign: login_users_path
|
45
|
+
users:
|
46
|
+
login:
|
47
|
+
ope: root_path
|
48
|
+
customer: root_path
|
49
|
+
tools:
|
50
|
+
default:
|
51
|
+
customer: 404
|
52
|
+
unsign: /tools/login
|
53
|
+
login:
|
54
|
+
ope: tools_path
|
55
|
+
customer: 404
|
56
|
+
```
|
57
|
+
|
58
|
+
The basic format for this redirect schema is like
|
59
|
+
|
60
|
+
```yaml
|
61
|
+
controller:
|
62
|
+
action:
|
63
|
+
user_role1: redirect_url
|
64
|
+
user_role2: redirect_url
|
65
|
+
user_role3: redirect_url
|
66
|
+
```
|
47
67
|
|
48
|
-
|
68
|
+
The `user_role` if the role attribute of your user model configed in
|
69
|
+
`config/application.rb` or `config/environments/*.rb`
|
70
|
+
`redirect_url` has 3 types
|
49
71
|
|
50
|
-
|
72
|
+
```
|
73
|
+
404 --> means raise a 404 error name 'Routing Error'
|
74
|
+
XXXX_path --> means using rails routing method
|
75
|
+
otherwise --> means using as absolute url string
|
76
|
+
```
|
51
77
|
|
52
|
-
Bug reports and pull requests are welcome on GitHub at https://github.com/[USERNAME]/easy_login. This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the [Contributor Covenant](contributor-covenant.org) code of conduct.
|
53
78
|
|
54
79
|
|
55
80
|
## License
|
data/lib/easy_login/config.rb
CHANGED
@@ -1,13 +1,19 @@
|
|
1
1
|
module EasyLogin
|
2
|
-
|
3
|
-
|
4
|
-
|
5
|
-
|
6
|
-
|
7
|
-
|
8
|
-
def self.salt
|
9
|
-
@@salt
|
10
|
-
end
|
2
|
+
class Config
|
3
|
+
attr_accessor :salt, :user_model, :user_role_attr
|
4
|
+
end
|
5
|
+
|
6
|
+
@@config = Config.new
|
11
7
|
|
12
|
-
|
8
|
+
def self.configed?
|
9
|
+
@@config.salt && @@config.user_model && @@config.user_role_attr
|
10
|
+
end
|
11
|
+
|
12
|
+
def self.config
|
13
|
+
@@config
|
14
|
+
end
|
15
|
+
|
16
|
+
def self.setup
|
17
|
+
yield @@config
|
18
|
+
end
|
13
19
|
end
|
@@ -0,0 +1,22 @@
|
|
1
|
+
module EasyLogin
|
2
|
+
class Redirect
|
3
|
+
def self.parse(controller_ins)
|
4
|
+
per YAML.load_file Rails.root.join "config", "redirect_to.yml"
|
5
|
+
controller = controller_ins.params[:controller]
|
6
|
+
action = controller_ins.params[:action]
|
7
|
+
p = per[controller]
|
8
|
+
p = p[action] || p['default'] if p
|
9
|
+
p = per['default'] unless p
|
10
|
+
if controller_ins.signed_in?
|
11
|
+
url = p[controller_ins.current_user.send(EasyLogin.config.user_role_attr)]
|
12
|
+
else
|
13
|
+
url = p['unsign']
|
14
|
+
end
|
15
|
+
if url
|
16
|
+
raise ActionController::RoutingError.new("Routing Error") if url == 404
|
17
|
+
url = controller_ins.send url if url.end_with? "path"
|
18
|
+
controller_ins.redirect_to url
|
19
|
+
end
|
20
|
+
end
|
21
|
+
end
|
22
|
+
end
|
data/lib/easy_login/session.rb
CHANGED
@@ -12,7 +12,7 @@ module EasyLogin
|
|
12
12
|
end
|
13
13
|
|
14
14
|
def sign_in(user)
|
15
|
-
cookies.signed[:f] = [user.id, EasyLogin
|
15
|
+
cookies.signed[:f] = [user.id, EasyLogin.config.salt]
|
16
16
|
end
|
17
17
|
|
18
18
|
def sign_out
|
@@ -27,13 +27,13 @@ module EasyLogin
|
|
27
27
|
def user_from_session_token
|
28
28
|
user_id = session_token[0]
|
29
29
|
return nil if user_id == nil
|
30
|
-
user =
|
30
|
+
user = EasyLogin.config.user_model.cap.capitalize.constantize.find_by_id(user_id)
|
31
31
|
return user
|
32
32
|
end
|
33
33
|
|
34
34
|
def session_token
|
35
35
|
session = cookies.signed[:f] || [nil, nil]
|
36
|
-
return [nil, session[1]] unless session[1] == EasyLogin
|
36
|
+
return [nil, session[1]] unless session[1] == EasyLogin.config.salt
|
37
37
|
session
|
38
38
|
end
|
39
39
|
end
|
data/lib/easy_login/version.rb
CHANGED
data/lib/easy_login.rb
CHANGED
@@ -1,9 +1,33 @@
|
|
1
1
|
require "easy_login/version"
|
2
2
|
require "easy_login/config"
|
3
3
|
require "easy_login/session"
|
4
|
+
require "easy_login/redirect"
|
4
5
|
|
5
6
|
module EasyLogin
|
6
7
|
def self.helper_method
|
7
8
|
[:signed_in?, :current_user, :current_user?]
|
8
9
|
end
|
10
|
+
|
11
|
+
class Railtie < Rails::Railtie
|
12
|
+
initializer "load this gem to controller" do
|
13
|
+
ApplicationController.send :include, EasyLogin
|
14
|
+
end
|
15
|
+
|
16
|
+
config.to_prepare do
|
17
|
+
unless EasyLogin.setup do |config|
|
18
|
+
config.salt = "salt_from_goshan"
|
19
|
+
config.user_model = "User"
|
20
|
+
config.user_role_attr = "role"
|
21
|
+
end
|
22
|
+
end
|
23
|
+
end
|
24
|
+
|
25
|
+
def self.included(base)
|
26
|
+
base.send :include, Session
|
27
|
+
base.helper_method EasyLogin.helper_method
|
28
|
+
base.send :before_action do |controller|
|
29
|
+
Redirect.parse controller
|
30
|
+
end
|
31
|
+
end
|
32
|
+
|
9
33
|
end
|
Binary file
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: easy_login
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.
|
4
|
+
version: 1.0.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- goshan
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2016-
|
11
|
+
date: 2016-07-04 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: bundler
|
@@ -55,11 +55,13 @@ files:
|
|
55
55
|
- easy_login.gemspec
|
56
56
|
- lib/easy_login.rb
|
57
57
|
- lib/easy_login/config.rb
|
58
|
+
- lib/easy_login/redirect.rb
|
58
59
|
- lib/easy_login/session.rb
|
59
60
|
- lib/easy_login/version.rb
|
60
61
|
- pkg/easy_login-0.1.0.gem
|
61
62
|
- pkg/easy_login-0.1.1.gem
|
62
63
|
- pkg/easy_login-0.1.2.gem
|
64
|
+
- pkg/easy_login-0.1.3.gem
|
63
65
|
homepage: https://github.com/goshan/easy_login
|
64
66
|
licenses:
|
65
67
|
- MIT
|