adeia 0.10.5 → 0.11.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: fbdf51a3d2163fc183b52129acad7c9006398c25
4
- data.tar.gz: 31e48a7ee399b4104016ba87b3491b4c4ae67701
3
+ metadata.gz: 4a1ebe192b9a6f72013ede46d4c0c4e0c5216948
4
+ data.tar.gz: 10f965b7685d483496fc0a774e888572dcc2532e
5
5
  SHA512:
6
- metadata.gz: 51950fe0201a51b93577e3aa7bbecf28cdf1baa8e293c02cb5d7739da42702a190ae907b76162d679786f990708b5bfb107f8edb3683c011e6e4e80b68fce9e3
7
- data.tar.gz: a276223d46974bbe5327ad91d90b84a8c701faf37bdc5068ef8b1575fe65ad3d01ff467b15fd5230a694db253137780f25d2321b761d496d106c1eda9c095148
6
+ metadata.gz: c9f8cef57a7f79abc2c08ca7b113a8da305aecfdabdf064e817dc1b9ef3f1f22e56d85ddabbd6f8e3437ef8da55bfda62f42a69394c86f6ca3d1cf3ee62a484e
7
+ data.tar.gz: 00af089afb586476ea97bf640fe32901da4a3a2ae9cdb4f7b911e052007e884e8aa737bfb5fab5d2fb18317f9401c85d0899a24b8a709cdba96ca0b31f8cdaf0
data/README.md CHANGED
@@ -2,9 +2,145 @@
2
2
 
3
3
  An authorization gem for Rails that allows you to have the complete control of your app.
4
4
 
5
+ ## Installation
6
+
7
+ Add this line to your application's Gemfile:
8
+
9
+ ```ruby
10
+ gem 'adeia'
11
+ ```
12
+
13
+ And then execute:
14
+
15
+ $ bundle
16
+
17
+ Or install it yourself as:
18
+
19
+ $ gem install adeia
20
+
21
+ Then include the engine's routes in your `routes.rb`. The URL on which you mount the engine is up to you.
22
+
23
+ ```ruby
24
+ # routes.rb
25
+
26
+ mount Adeia::Engine => "/adeia"
27
+ ```
28
+
5
29
  ## Requirements
6
30
 
7
31
  Requires a User model with:
8
32
 
9
33
  * An method `name`, returning the name of the user.
10
- * A column `remember_token`, containing a generated token. It is used for the authentification.
34
+ * A column `remember_token`, containing a generated token used for the authentification.
35
+
36
+ ## Documentation
37
+
38
+ ### Authentification
39
+
40
+ Adeia provides methods to sign in and out, to get or set the current user and to check if a user is signed in.
41
+
42
+ ```ruby
43
+
44
+ # sign in an user
45
+ sign_in @user
46
+ # alternatively, sign in permanently
47
+ sign_in @user, permanent: true
48
+
49
+ # get and set the connected user
50
+ current_user # => #<User>
51
+ current_user = @an_other_user
52
+
53
+ # check if the user is signed in
54
+ if signed_in?
55
+ # Do stuff
56
+ end
57
+
58
+ ```
59
+
60
+ ### Authorization
61
+
62
+ There are four different authorization methods at action-level.
63
+
64
+ `require_login!` checks if the user is signed in. It raises the exception `LoginRequired` if not.
65
+
66
+ ```ruby
67
+ def index
68
+ require_login!
69
+ @events = Event.all
70
+ end
71
+ ```
72
+
73
+ `authorize!` checks if the user has the permissions to access the action. It raises `AccessDenied` if not.
74
+
75
+ ```ruby
76
+ def new
77
+ authorize!
78
+ @event = Event.new
79
+ end
80
+ ```
81
+
82
+ `load_and_authorize!` loads the suitable record and checks if the user has the permissions to access the action, taking into account the loaded record. It raises `AccessDenied` if not.
83
+ The method returns the record, but it also automatically set an instance variable named after the model.
84
+
85
+ ```ruby
86
+ def edit
87
+ @event = load_and_authorize!
88
+ # assignation is optional here
89
+ end
90
+ ```
91
+
92
+ `authorize_and_load_records!` loads the records taking into account the user's permissions. It raises `AccessDenied` if the user hasn't access to any records.
93
+
94
+ ```ruby
95
+ def index
96
+ @events = authorize_and_load_records!
97
+ # assignation is optional here
98
+ end
99
+ ```
100
+
101
+ By default, the methods (except `require_login!`) use the following parameters:
102
+
103
+ * controller: the controller's name
104
+ * action: the action's name
105
+ * token: GET parameter `token`
106
+ * resource: fetch the resource from controller's name
107
+
108
+ You can override those parameters when invoking the method:
109
+
110
+ ```ruby
111
+ def index
112
+ authorize!(controller: 'events', action: 'new')
113
+ end
114
+ ```
115
+ Adeia also provide controller-level methods to keep your code DRY.
116
+
117
+ `require_login` adds the `require_login!` method to the controller's actions.
118
+
119
+ `load_and_authorize` adds the suitable methods to the controller's actions:
120
+
121
+ * index: `authorize_and_load_records!`
122
+ * show, edit, update, destroy: `load_and_authorize!`
123
+ * new, create, other actions: `authorize!`
124
+
125
+ The two controller-level methods accepts the restricting parameters `only` and `except`.
126
+
127
+ ```ruby
128
+ class EventsController < ApplicationController
129
+
130
+ require_login only: [:postpone]
131
+ load_and_authorize, except: [:postpone]
132
+
133
+ def index; end
134
+
135
+ def new; end
136
+
137
+ def create; end
138
+
139
+ def postpone; end
140
+
141
+ end
142
+ ```
143
+
144
+ ### Other methods
145
+
146
+ When an authorization exception is raised by the engine, it automatically store the current user's location in a cookie. The called method is `store_location` and is available in your controllers. Then you can use the method `redirect_back_or(default, message = nil)` which either redirects to the stored location if any or redirects the default provided path, with an optional message.
@@ -35,6 +35,11 @@ module Adeia
35
35
  return controller_resource.load_records
36
36
  end
37
37
 
38
+ def load_records(**args)
39
+ controller_resource = ControllerResource.new(self, **args)
40
+ return controller_resource.load_records
41
+ end
42
+
38
43
  def authorize!(**args)
39
44
  ControllerResource.new(self, **args).authorize!
40
45
  end
@@ -55,7 +60,7 @@ module Adeia
55
60
  end
56
61
 
57
62
  # Redirect the user to the stored url or the default one provided
58
- #
63
+ #
59
64
  # * *Args* :
60
65
  # - default path to redirect to
61
66
  # * *Returns* :
@@ -66,9 +71,9 @@ module Adeia
66
71
  end
67
72
 
68
73
  # Store the current url in a cookie
69
- #
74
+ #
70
75
  # * *Args* :
71
- #
76
+ #
72
77
  # * *Returns* :
73
78
  #
74
79
  def store_location
@@ -77,4 +82,4 @@ module Adeia
77
82
 
78
83
  end
79
84
 
80
- end
85
+ end
data/lib/adeia/version.rb CHANGED
@@ -1,3 +1,3 @@
1
1
  module Adeia
2
- VERSION = "0.10.5"
2
+ VERSION = "0.11.0"
3
3
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: adeia
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.10.5
4
+ version: 0.11.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - khcr
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2016-01-10 00:00:00.000000000 Z
11
+ date: 2016-02-10 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: rails