rodauth-guest 0.0.1
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/CHANGELOG.md +5 -0
- data/LICENSE.txt +21 -0
- data/README.md +114 -0
- data/lib/rodauth/features/guest.rb +96 -0
- data/rodauth-guest.gemspec +30 -0
- metadata +176 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA256:
|
3
|
+
metadata.gz: 6b5f40c83280574e9221b0544ac1f3c9f881366bbe4530046c8a34d0351b17b7
|
4
|
+
data.tar.gz: bfe981dc8f345ac23c7f8899a6ba8067b5105fe43f13a34bc86e3ac900c823c8
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: 2f4e12f4c6e1126b022707b223991da0d9885dee74cc50277f7e6f31c11057d5b66d922d04684028563ed81a5e84af7df7e7bbcd2b90fb86e5c0e40b37cc4a70
|
7
|
+
data.tar.gz: 8f4113bd20e7f793d2cc15450209a773c6a2943d68aeb65c03bc4ecc7094e3a8e3fcba919f8a811d5f8b7055389f8640e8a6da25734df268c5bb222d65078329
|
data/CHANGELOG.md
ADDED
data/LICENSE.txt
ADDED
@@ -0,0 +1,21 @@
|
|
1
|
+
The MIT License (MIT)
|
2
|
+
|
3
|
+
Copyright (c) 2022 Janko Marohnić
|
4
|
+
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
7
|
+
in the Software without restriction, including without limitation the rights
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
10
|
+
furnished to do so, subject to the following conditions:
|
11
|
+
|
12
|
+
The above copyright notice and this permission notice shall be included in
|
13
|
+
all copies or substantial portions of the Software.
|
14
|
+
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
21
|
+
THE SOFTWARE.
|
data/README.md
ADDED
@@ -0,0 +1,114 @@
|
|
1
|
+
# rodauth-guest
|
2
|
+
|
3
|
+
Provides guest user functionality for [Rodauth].
|
4
|
+
|
5
|
+
## Installation
|
6
|
+
|
7
|
+
Add the gem to your project:
|
8
|
+
|
9
|
+
```sh
|
10
|
+
$ bundle add rodauth-guest
|
11
|
+
```
|
12
|
+
|
13
|
+
## Usage
|
14
|
+
|
15
|
+
Start by enabling the `guest` feature in your Rodauth configuration:
|
16
|
+
|
17
|
+
```rb
|
18
|
+
plugin :rodauth do
|
19
|
+
enable :guest
|
20
|
+
end
|
21
|
+
```
|
22
|
+
|
23
|
+
The feature provides the ability to automatically create and log in anonymous accounts when no account is not logged in. You just need to choose for which routes you want to allow guest users:
|
24
|
+
|
25
|
+
```rb
|
26
|
+
route do |r|
|
27
|
+
r.rodauth # route rodauth requests
|
28
|
+
|
29
|
+
if r.path.start_with?("/dashboard")
|
30
|
+
rodauth.allow_guest
|
31
|
+
end
|
32
|
+
end
|
33
|
+
```
|
34
|
+
|
35
|
+
The guest account will be logged in the same way normal accounts are, so `logged_in?` & `authenticated?` will return `true`, and `session_value` will return the guest account ID. You can check whether a guest account is logged in:
|
36
|
+
|
37
|
+
```rb
|
38
|
+
if rodauth.guest_logged_in?
|
39
|
+
# ...
|
40
|
+
end
|
41
|
+
```
|
42
|
+
|
43
|
+
### Guest creation
|
44
|
+
|
45
|
+
Guest accounts are unverified by default, and have a random email address in the form of `guest_<RANDOM_STRING>@example.com`.
|
46
|
+
|
47
|
+
```rb
|
48
|
+
rodauth.account_from_session
|
49
|
+
rodauth.account #=> { id: 1, status_id: 1, email: "guest_32978759-77bc-4293-ab8f-f1b96b9f178b@example.com" }
|
50
|
+
```
|
51
|
+
|
52
|
+
You can set additional column data on guest account creation:
|
53
|
+
|
54
|
+
```rb
|
55
|
+
# in a schema migration:
|
56
|
+
add_column :accounts, :guest, :boolean, default: true
|
57
|
+
```
|
58
|
+
```rb
|
59
|
+
before_create_guest do
|
60
|
+
account[:guest] = true
|
61
|
+
end
|
62
|
+
```
|
63
|
+
|
64
|
+
To override new email generation:
|
65
|
+
|
66
|
+
```rb
|
67
|
+
new_guest_login { "guest_#{SecureRandom.hex}@example.com" }
|
68
|
+
```
|
69
|
+
|
70
|
+
Or just the unique suffix:
|
71
|
+
|
72
|
+
```rb
|
73
|
+
guest_unique_suffix { SecureRandom.hex }
|
74
|
+
```
|
75
|
+
|
76
|
+
### Guest deletion
|
77
|
+
|
78
|
+
The logged in guest account is automatically deleted when a new account is created. You can use a before hook to transfer any data from the guest account into the new user:
|
79
|
+
|
80
|
+
```rb
|
81
|
+
before_delete_guest do
|
82
|
+
# session_value - guest account ID
|
83
|
+
# account_id - new account ID
|
84
|
+
db[:articles].where(account_id: session_value).update(account_id: account_id)
|
85
|
+
end
|
86
|
+
```
|
87
|
+
|
88
|
+
You can also skip deletion of the guest record on account creation (by default it's skipped when `create_account_autologin?` is `false`):
|
89
|
+
|
90
|
+
```rb
|
91
|
+
delete_guest_on_create? false
|
92
|
+
```
|
93
|
+
|
94
|
+
## Development
|
95
|
+
|
96
|
+
Run tests with Rake:
|
97
|
+
|
98
|
+
```sh
|
99
|
+
$ bundle exec rake test
|
100
|
+
```
|
101
|
+
|
102
|
+
## Contributing
|
103
|
+
|
104
|
+
Bug reports and pull requests are welcome on GitHub at https://github.com/janko/rodauth-guest. This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the [code of conduct](https://github.com/janko/rodauth-guest/blob/main/CODE_OF_CONDUCT.md).
|
105
|
+
|
106
|
+
## License
|
107
|
+
|
108
|
+
The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
|
109
|
+
|
110
|
+
## Code of Conduct
|
111
|
+
|
112
|
+
Everyone interacting in the Rodauth::Guest project's codebases, issue trackers, chat rooms and mailing lists is expected to follow the [code of conduct](https://github.com/janko/rodauth-guest/blob/main/CODE_OF_CONDUCT.md).
|
113
|
+
|
114
|
+
[Rodauth]: https://github.com/jeremyevans/rodauth
|
@@ -0,0 +1,96 @@
|
|
1
|
+
require "securerandom"
|
2
|
+
|
3
|
+
module Rodauth
|
4
|
+
Feature.define(:guest, :Guest) do
|
5
|
+
before "create_guest"
|
6
|
+
after "create_guest"
|
7
|
+
before "delete_guest"
|
8
|
+
after "delete_guest"
|
9
|
+
|
10
|
+
auth_value_methods :delete_guest_on_create?
|
11
|
+
|
12
|
+
auth_methods(
|
13
|
+
:guest_logged_in?,
|
14
|
+
:create_guest,
|
15
|
+
:save_guest,
|
16
|
+
:delete_guest,
|
17
|
+
:new_guest_login,
|
18
|
+
:guest_unique_suffix,
|
19
|
+
)
|
20
|
+
|
21
|
+
auth_private_methods(
|
22
|
+
:new_guest,
|
23
|
+
)
|
24
|
+
|
25
|
+
def allow_guest
|
26
|
+
@guest_allowed = true
|
27
|
+
return if logged_in?
|
28
|
+
|
29
|
+
create_guest
|
30
|
+
login_session("guest")
|
31
|
+
end
|
32
|
+
|
33
|
+
def logged_in?
|
34
|
+
super unless guest_logged_in? && !guest_allowed?
|
35
|
+
end
|
36
|
+
|
37
|
+
def create_guest
|
38
|
+
new_guest
|
39
|
+
before_create_guest
|
40
|
+
save_guest
|
41
|
+
after_create_guest
|
42
|
+
end
|
43
|
+
|
44
|
+
def new_guest
|
45
|
+
@account = _new_guest
|
46
|
+
end
|
47
|
+
|
48
|
+
def save_guest
|
49
|
+
account[account_id_column] = db[accounts_table].insert(account)
|
50
|
+
end
|
51
|
+
|
52
|
+
def guest_logged_in?
|
53
|
+
authenticated_by && authenticated_by.include?("guest")
|
54
|
+
end
|
55
|
+
|
56
|
+
private
|
57
|
+
|
58
|
+
def after_create_account
|
59
|
+
super if defined?(super)
|
60
|
+
if guest_logged_in? && delete_guest_on_create?
|
61
|
+
before_delete_guest
|
62
|
+
delete_guest
|
63
|
+
after_delete_guest
|
64
|
+
end
|
65
|
+
end
|
66
|
+
|
67
|
+
def delete_guest
|
68
|
+
account_ds(session_value).delete
|
69
|
+
end
|
70
|
+
|
71
|
+
def delete_guest_on_create?
|
72
|
+
create_account_autologin?
|
73
|
+
end
|
74
|
+
|
75
|
+
def account_session_status_filter
|
76
|
+
return {} if guest_logged_in?
|
77
|
+
super
|
78
|
+
end
|
79
|
+
|
80
|
+
def guest_allowed?
|
81
|
+
@guest_allowed
|
82
|
+
end
|
83
|
+
|
84
|
+
def _new_guest
|
85
|
+
{ login_column => new_guest_login }
|
86
|
+
end
|
87
|
+
|
88
|
+
def new_guest_login
|
89
|
+
"guest_#{guest_unique_suffix}@example.com"
|
90
|
+
end
|
91
|
+
|
92
|
+
def guest_unique_suffix
|
93
|
+
SecureRandom.uuid
|
94
|
+
end
|
95
|
+
end
|
96
|
+
end
|
@@ -0,0 +1,30 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
Gem::Specification.new do |spec|
|
4
|
+
spec.name = "rodauth-guest"
|
5
|
+
spec.version = "0.0.1"
|
6
|
+
spec.authors = ["Janko Marohnić"]
|
7
|
+
spec.email = ["janko@hey.com"]
|
8
|
+
|
9
|
+
spec.summary = "Provides guest users functionality for Rodauth."
|
10
|
+
spec.homepage = "https://github.com/janko/rodauth-guest"
|
11
|
+
spec.license = "MIT"
|
12
|
+
spec.required_ruby_version = ">= 2.3"
|
13
|
+
|
14
|
+
spec.metadata["homepage_uri"] = spec.homepage
|
15
|
+
spec.metadata["source_code_uri"] = "https://github.com/janko/rodauth-guest"
|
16
|
+
|
17
|
+
spec.files = Dir["lib/**/*", "README.md", "CHANGELOG.md", "LICENSE.txt", "*.gemspec"]
|
18
|
+
spec.require_paths = ["lib"]
|
19
|
+
|
20
|
+
spec.add_dependency "rodauth", "~> 2.0"
|
21
|
+
|
22
|
+
spec.add_development_dependency "minitest"
|
23
|
+
spec.add_development_dependency "minitest-hooks"
|
24
|
+
spec.add_development_dependency "capybara"
|
25
|
+
spec.add_development_dependency "sqlite3"
|
26
|
+
spec.add_development_dependency "tilt"
|
27
|
+
spec.add_development_dependency "bcrypt"
|
28
|
+
spec.add_development_dependency "mail"
|
29
|
+
spec.add_development_dependency "net-smtp"
|
30
|
+
end
|
metadata
ADDED
@@ -0,0 +1,176 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: rodauth-guest
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.0.1
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Janko Marohnić
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
date: 2022-10-11 00:00:00.000000000 Z
|
12
|
+
dependencies:
|
13
|
+
- !ruby/object:Gem::Dependency
|
14
|
+
name: rodauth
|
15
|
+
requirement: !ruby/object:Gem::Requirement
|
16
|
+
requirements:
|
17
|
+
- - "~>"
|
18
|
+
- !ruby/object:Gem::Version
|
19
|
+
version: '2.0'
|
20
|
+
type: :runtime
|
21
|
+
prerelease: false
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - "~>"
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: '2.0'
|
27
|
+
- !ruby/object:Gem::Dependency
|
28
|
+
name: minitest
|
29
|
+
requirement: !ruby/object:Gem::Requirement
|
30
|
+
requirements:
|
31
|
+
- - ">="
|
32
|
+
- !ruby/object:Gem::Version
|
33
|
+
version: '0'
|
34
|
+
type: :development
|
35
|
+
prerelease: false
|
36
|
+
version_requirements: !ruby/object:Gem::Requirement
|
37
|
+
requirements:
|
38
|
+
- - ">="
|
39
|
+
- !ruby/object:Gem::Version
|
40
|
+
version: '0'
|
41
|
+
- !ruby/object:Gem::Dependency
|
42
|
+
name: minitest-hooks
|
43
|
+
requirement: !ruby/object:Gem::Requirement
|
44
|
+
requirements:
|
45
|
+
- - ">="
|
46
|
+
- !ruby/object:Gem::Version
|
47
|
+
version: '0'
|
48
|
+
type: :development
|
49
|
+
prerelease: false
|
50
|
+
version_requirements: !ruby/object:Gem::Requirement
|
51
|
+
requirements:
|
52
|
+
- - ">="
|
53
|
+
- !ruby/object:Gem::Version
|
54
|
+
version: '0'
|
55
|
+
- !ruby/object:Gem::Dependency
|
56
|
+
name: capybara
|
57
|
+
requirement: !ruby/object:Gem::Requirement
|
58
|
+
requirements:
|
59
|
+
- - ">="
|
60
|
+
- !ruby/object:Gem::Version
|
61
|
+
version: '0'
|
62
|
+
type: :development
|
63
|
+
prerelease: false
|
64
|
+
version_requirements: !ruby/object:Gem::Requirement
|
65
|
+
requirements:
|
66
|
+
- - ">="
|
67
|
+
- !ruby/object:Gem::Version
|
68
|
+
version: '0'
|
69
|
+
- !ruby/object:Gem::Dependency
|
70
|
+
name: sqlite3
|
71
|
+
requirement: !ruby/object:Gem::Requirement
|
72
|
+
requirements:
|
73
|
+
- - ">="
|
74
|
+
- !ruby/object:Gem::Version
|
75
|
+
version: '0'
|
76
|
+
type: :development
|
77
|
+
prerelease: false
|
78
|
+
version_requirements: !ruby/object:Gem::Requirement
|
79
|
+
requirements:
|
80
|
+
- - ">="
|
81
|
+
- !ruby/object:Gem::Version
|
82
|
+
version: '0'
|
83
|
+
- !ruby/object:Gem::Dependency
|
84
|
+
name: tilt
|
85
|
+
requirement: !ruby/object:Gem::Requirement
|
86
|
+
requirements:
|
87
|
+
- - ">="
|
88
|
+
- !ruby/object:Gem::Version
|
89
|
+
version: '0'
|
90
|
+
type: :development
|
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: bcrypt
|
99
|
+
requirement: !ruby/object:Gem::Requirement
|
100
|
+
requirements:
|
101
|
+
- - ">="
|
102
|
+
- !ruby/object:Gem::Version
|
103
|
+
version: '0'
|
104
|
+
type: :development
|
105
|
+
prerelease: false
|
106
|
+
version_requirements: !ruby/object:Gem::Requirement
|
107
|
+
requirements:
|
108
|
+
- - ">="
|
109
|
+
- !ruby/object:Gem::Version
|
110
|
+
version: '0'
|
111
|
+
- !ruby/object:Gem::Dependency
|
112
|
+
name: mail
|
113
|
+
requirement: !ruby/object:Gem::Requirement
|
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
|
+
requirements:
|
122
|
+
- - ">="
|
123
|
+
- !ruby/object:Gem::Version
|
124
|
+
version: '0'
|
125
|
+
- !ruby/object:Gem::Dependency
|
126
|
+
name: net-smtp
|
127
|
+
requirement: !ruby/object:Gem::Requirement
|
128
|
+
requirements:
|
129
|
+
- - ">="
|
130
|
+
- !ruby/object:Gem::Version
|
131
|
+
version: '0'
|
132
|
+
type: :development
|
133
|
+
prerelease: false
|
134
|
+
version_requirements: !ruby/object:Gem::Requirement
|
135
|
+
requirements:
|
136
|
+
- - ">="
|
137
|
+
- !ruby/object:Gem::Version
|
138
|
+
version: '0'
|
139
|
+
description:
|
140
|
+
email:
|
141
|
+
- janko@hey.com
|
142
|
+
executables: []
|
143
|
+
extensions: []
|
144
|
+
extra_rdoc_files: []
|
145
|
+
files:
|
146
|
+
- CHANGELOG.md
|
147
|
+
- LICENSE.txt
|
148
|
+
- README.md
|
149
|
+
- lib/rodauth/features/guest.rb
|
150
|
+
- rodauth-guest.gemspec
|
151
|
+
homepage: https://github.com/janko/rodauth-guest
|
152
|
+
licenses:
|
153
|
+
- MIT
|
154
|
+
metadata:
|
155
|
+
homepage_uri: https://github.com/janko/rodauth-guest
|
156
|
+
source_code_uri: https://github.com/janko/rodauth-guest
|
157
|
+
post_install_message:
|
158
|
+
rdoc_options: []
|
159
|
+
require_paths:
|
160
|
+
- lib
|
161
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
162
|
+
requirements:
|
163
|
+
- - ">="
|
164
|
+
- !ruby/object:Gem::Version
|
165
|
+
version: '2.3'
|
166
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
167
|
+
requirements:
|
168
|
+
- - ">="
|
169
|
+
- !ruby/object:Gem::Version
|
170
|
+
version: '0'
|
171
|
+
requirements: []
|
172
|
+
rubygems_version: 3.3.3
|
173
|
+
signing_key:
|
174
|
+
specification_version: 4
|
175
|
+
summary: Provides guest users functionality for Rodauth.
|
176
|
+
test_files: []
|