plutonium 0.15.5 → 0.15.6
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/app/assets/plutonium.css +1 -1
- data/app/assets/plutonium.ico +0 -0
- data/app/assets/plutonium.png +0 -0
- data/docs/.vitepress/config.ts +61 -0
- data/docs/.vitepress/theme/custom.css +61 -0
- data/docs/.vitepress/theme/index.ts +4 -0
- data/docs/api-examples.md +49 -0
- data/docs/guide/getting-started/authorization.md +296 -0
- data/docs/guide/getting-started/core-concepts.md +432 -0
- data/docs/guide/getting-started/index.md +18 -0
- data/docs/guide/getting-started/installation.md +269 -0
- data/docs/guide/getting-started/resources.md +254 -0
- data/docs/guide/what-is-plutonium.md +211 -0
- data/docs/index.md +43 -0
- data/docs/markdown-examples.md +85 -0
- data/docs/public/android-chrome-192x192.png +0 -0
- data/docs/public/android-chrome-512x512.png +0 -0
- data/docs/public/apple-touch-icon.png +0 -0
- data/docs/public/favicon-16x16.png +0 -0
- data/docs/public/favicon-32x32.png +0 -0
- data/docs/public/favicon.ico +0 -0
- data/docs/public/plutonium.png +0 -0
- data/docs/public/site.webmanifest +1 -0
- data/docs/public/templates/plutonium.rb +21 -0
- data/lib/generators/pu/core/assets/assets_generator.rb +2 -3
- data/lib/generators/pu/core/assets/templates/tailwind.config.js +2 -2
- data/lib/generators/pu/core/install/install_generator.rb +9 -1
- data/lib/generators/pu/core/install/templates/config/initializers/plutonium.rb +0 -1
- data/lib/plutonium/core/controllers/authorizable.rb +1 -1
- data/lib/plutonium/railtie.rb +0 -10
- data/lib/plutonium/resource/controllers/crud_actions.rb +1 -1
- data/lib/plutonium/resource/policy.rb +4 -5
- data/lib/plutonium/resource/register.rb +3 -0
- data/lib/plutonium/ui/action_button.rb +34 -19
- data/lib/plutonium/ui/form/resource.rb +2 -1
- data/lib/plutonium/ui/table/components/search_bar.rb +1 -1
- data/lib/plutonium/version.rb +1 -1
- data/package-lock.json +5767 -1851
- data/package.json +10 -4
- data/src/js/core.js +0 -1
- data/tailwind.options.js +89 -11
- metadata +27 -11
- data/app/assets/plutonium-original.png +0 -0
- data/app/assets/plutonium-white.png +0 -0
- data/public/plutonium-assets/fonts/bootstrap-icons.woff +0 -0
- data/public/plutonium-assets/fonts/bootstrap-icons.woff2 +0 -0
- /data/{templates → docs/public/templates}/base.rb +0 -0
@@ -0,0 +1,49 @@
|
|
1
|
+
---
|
2
|
+
outline: deep
|
3
|
+
---
|
4
|
+
|
5
|
+
# Runtime API Examples
|
6
|
+
|
7
|
+
This page demonstrates usage of some of the runtime APIs provided by VitePress.
|
8
|
+
|
9
|
+
The main `useData()` API can be used to access site, theme, and page data for the current page. It works in both `.md` and `.vue` files:
|
10
|
+
|
11
|
+
```md
|
12
|
+
<script setup>
|
13
|
+
import { useData } from 'vitepress'
|
14
|
+
|
15
|
+
const { theme, page, frontmatter } = useData()
|
16
|
+
</script>
|
17
|
+
|
18
|
+
## Results
|
19
|
+
|
20
|
+
### Theme Data
|
21
|
+
<pre>{{ theme }}</pre>
|
22
|
+
|
23
|
+
### Page Data
|
24
|
+
<pre>{{ page }}</pre>
|
25
|
+
|
26
|
+
### Page Frontmatter
|
27
|
+
<pre>{{ frontmatter }}</pre>
|
28
|
+
```
|
29
|
+
|
30
|
+
<script setup>
|
31
|
+
import { useData } from 'vitepress'
|
32
|
+
|
33
|
+
const { site, theme, page, frontmatter } = useData()
|
34
|
+
</script>
|
35
|
+
|
36
|
+
## Results
|
37
|
+
|
38
|
+
### Theme Data
|
39
|
+
<pre>{{ theme }}</pre>
|
40
|
+
|
41
|
+
### Page Data
|
42
|
+
<pre>{{ page }}</pre>
|
43
|
+
|
44
|
+
### Page Frontmatter
|
45
|
+
<pre>{{ frontmatter }}</pre>
|
46
|
+
|
47
|
+
## More
|
48
|
+
|
49
|
+
Check out the documentation for the [full list of runtime APIs](https://vitepress.dev/reference/runtime-api#usedata).
|
@@ -0,0 +1,296 @@
|
|
1
|
+
# Authorization and Access Control
|
2
|
+
|
3
|
+
Plutonium provides a robust authorization system built on top of [Action Policy](https://actionpolicy.evilmartians.io/), offering fine-grained access control, resource scoping, and entity-based authorization.
|
4
|
+
|
5
|
+
## Overview
|
6
|
+
|
7
|
+
Authorization in Plutonium operates at multiple levels:
|
8
|
+
- Resource-level policies
|
9
|
+
- Action-based permissions
|
10
|
+
- Entity-based scoping
|
11
|
+
- Attribute-level access control
|
12
|
+
|
13
|
+
## Basic Policy Definition
|
14
|
+
|
15
|
+
Every resource in Plutonium requires a policy. Here's a basic example:
|
16
|
+
|
17
|
+
```ruby
|
18
|
+
class BlogPolicy < ResourcePolicy
|
19
|
+
# Core CRUD Permissions
|
20
|
+
|
21
|
+
def create?
|
22
|
+
# All authenticated users can create blogs
|
23
|
+
true
|
24
|
+
end
|
25
|
+
|
26
|
+
def read?
|
27
|
+
# Allow anyone to read blogs
|
28
|
+
true
|
29
|
+
end
|
30
|
+
|
31
|
+
def update?
|
32
|
+
# Allow only the blog owner to update
|
33
|
+
owner?
|
34
|
+
end
|
35
|
+
|
36
|
+
def destroy?
|
37
|
+
# Allow only the blog owner or admins to destroy
|
38
|
+
owner? || user.admin?
|
39
|
+
end
|
40
|
+
|
41
|
+
# Attribute Control
|
42
|
+
|
43
|
+
def permitted_attributes_for_create
|
44
|
+
[:title, :content, :category]
|
45
|
+
end
|
46
|
+
|
47
|
+
def permitted_attributes_for_read
|
48
|
+
[:title, :content, :category, :created_at, :updated_at, :user_id]
|
49
|
+
end
|
50
|
+
|
51
|
+
def permitted_attributes_for_update
|
52
|
+
[:title, :content, :category]
|
53
|
+
end
|
54
|
+
|
55
|
+
# Association Access
|
56
|
+
|
57
|
+
def permitted_associations
|
58
|
+
[:comments]
|
59
|
+
end
|
60
|
+
|
61
|
+
private
|
62
|
+
|
63
|
+
def owner?
|
64
|
+
record.user_id == user.id
|
65
|
+
end
|
66
|
+
end
|
67
|
+
```
|
68
|
+
|
69
|
+
## Default Behaviors in Plutonium Policies
|
70
|
+
|
71
|
+
::: info Overview
|
72
|
+
Plutonium's Policy system implements a secure-by-default pattern with clear inheritance chains for both permissions and attribute access.
|
73
|
+
:::
|
74
|
+
|
75
|
+
### Permission Defaults
|
76
|
+
|
77
|
+
Permission methods follow two key patterns: core permissions that default to `false`, and derived permissions that inherit from core ones.
|
78
|
+
|
79
|
+
#### Core Permission Chain
|
80
|
+
|
81
|
+
```mermaid
|
82
|
+
graph TD
|
83
|
+
subgraph Core Permissions
|
84
|
+
A[create? ❌] --> B[update? ❌]
|
85
|
+
A --> C[destroy? ❌]
|
86
|
+
D[read? ❌] --> E[index? ❌]
|
87
|
+
D --> F[show? ❌]
|
88
|
+
end
|
89
|
+
```
|
90
|
+
|
91
|
+
::: warning Security First
|
92
|
+
All core permissions (`create?` and `read?`) default to `false`. You must explicitly override these to grant access.
|
93
|
+
```ruby
|
94
|
+
# Default implementations
|
95
|
+
def create?
|
96
|
+
false
|
97
|
+
end
|
98
|
+
|
99
|
+
def read?
|
100
|
+
false
|
101
|
+
end
|
102
|
+
```
|
103
|
+
:::
|
104
|
+
|
105
|
+
#### Permission Inheritance
|
106
|
+
|
107
|
+
::: code-group
|
108
|
+
```ruby [Action Methods]
|
109
|
+
def update?
|
110
|
+
create? # Inherits from create?
|
111
|
+
end
|
112
|
+
|
113
|
+
def destroy?
|
114
|
+
create? # Inherits from create?
|
115
|
+
end
|
116
|
+
|
117
|
+
def index?
|
118
|
+
read? # Inherits from read?
|
119
|
+
end
|
120
|
+
|
121
|
+
def show?
|
122
|
+
read? # Inherits from read?
|
123
|
+
end
|
124
|
+
```
|
125
|
+
|
126
|
+
```ruby [Helper Methods]
|
127
|
+
def new?
|
128
|
+
create? # Matches create?
|
129
|
+
end
|
130
|
+
|
131
|
+
def edit?
|
132
|
+
update? # Matches update?
|
133
|
+
end
|
134
|
+
|
135
|
+
def search?
|
136
|
+
index? # Matches index?
|
137
|
+
end
|
138
|
+
```
|
139
|
+
:::
|
140
|
+
|
141
|
+
### Attribute Permission Defaults
|
142
|
+
|
143
|
+
Attribute permissions also follow an inheritance pattern, but with auto-detection in development:
|
144
|
+
|
145
|
+
::: details Inheritance Chain
|
146
|
+
```mermaid
|
147
|
+
graph TD
|
148
|
+
subgraph Core Attributes
|
149
|
+
A[permitted_attributes_for_create] --> B[permitted_attributes_for_update]
|
150
|
+
C[permitted_attributes_for_read] --> D[permitted_attributes_for_show]
|
151
|
+
C --> E[permitted_attributes_for_index]
|
152
|
+
end
|
153
|
+
```
|
154
|
+
:::
|
155
|
+
|
156
|
+
#### Core Implementation Details
|
157
|
+
|
158
|
+
::: code-group
|
159
|
+
```ruby [Create Attributes]
|
160
|
+
def permitted_attributes_for_create
|
161
|
+
# Auto-detects fields but excludes system columns
|
162
|
+
autodetect_permitted_fields(:permitted_attributes_for_create) - [
|
163
|
+
resource_class.primary_key.to_sym,
|
164
|
+
:created_at,
|
165
|
+
:updated_at
|
166
|
+
]
|
167
|
+
end
|
168
|
+
```
|
169
|
+
|
170
|
+
```ruby [Read Attributes]
|
171
|
+
def permitted_attributes_for_read
|
172
|
+
# Auto-detects all fields
|
173
|
+
autodetect_permitted_fields(:permitted_attributes_for_read)
|
174
|
+
end
|
175
|
+
```
|
176
|
+
|
177
|
+
```ruby [Update Attributes]
|
178
|
+
def permitted_attributes_for_update
|
179
|
+
# Inherits from create
|
180
|
+
permitted_attributes_for_create
|
181
|
+
end
|
182
|
+
```
|
183
|
+
:::
|
184
|
+
|
185
|
+
::: danger Auto-detection Warning
|
186
|
+
The default attribute detection:
|
187
|
+
- Only works in development
|
188
|
+
- Raises errors in other environments
|
189
|
+
- Shows warning messages
|
190
|
+
- Must be overridden in production
|
191
|
+
:::
|
192
|
+
|
193
|
+
### Association Defaults
|
194
|
+
|
195
|
+
By default, no associations are permitted:
|
196
|
+
|
197
|
+
```ruby
|
198
|
+
def permitted_associations
|
199
|
+
[] # Must be explicitly defined
|
200
|
+
end
|
201
|
+
```
|
202
|
+
|
203
|
+
### Context Object Defaults
|
204
|
+
|
205
|
+
Two built-in authorization contexts:
|
206
|
+
|
207
|
+
::: code-group
|
208
|
+
```ruby [Required Context]
|
209
|
+
# User must be present
|
210
|
+
authorize :user, allow_nil: false
|
211
|
+
```
|
212
|
+
|
213
|
+
```ruby [Optional Context]
|
214
|
+
# Entity scope is optional
|
215
|
+
authorize :entity_scope, allow_nil: true
|
216
|
+
```
|
217
|
+
:::
|
218
|
+
|
219
|
+
#### Default Scoping Behavior
|
220
|
+
|
221
|
+
When an entity scope exists, records are automatically filtered:
|
222
|
+
|
223
|
+
```ruby
|
224
|
+
relation_scope do |relation|
|
225
|
+
next relation unless entity_scope
|
226
|
+
relation.associated_with(entity_scope)
|
227
|
+
end
|
228
|
+
```
|
229
|
+
|
230
|
+
#### Adding Custom Contexts
|
231
|
+
|
232
|
+
You can add additional authorization contexts:
|
233
|
+
|
234
|
+
::: code-group
|
235
|
+
```ruby [Policy]
|
236
|
+
class BlogPolicy < ResourcePolicy
|
237
|
+
authorize :ability, allow_nil: true
|
238
|
+
|
239
|
+
def promote?
|
240
|
+
user.admin? && ability&.can?(:promote, record)
|
241
|
+
end
|
242
|
+
end
|
243
|
+
```
|
244
|
+
|
245
|
+
```ruby [Controller]
|
246
|
+
class BlogsController < ResourceController
|
247
|
+
authorize :ability, through: :current_ability
|
248
|
+
|
249
|
+
private
|
250
|
+
|
251
|
+
def current_ability
|
252
|
+
@current_ability ||= Ability.new(current_user)
|
253
|
+
end
|
254
|
+
end
|
255
|
+
```
|
256
|
+
:::
|
257
|
+
|
258
|
+
## Quick Reference
|
259
|
+
|
260
|
+
| Method Type | Default | Inherits From |
|
261
|
+
|------------|---------|---------------|
|
262
|
+
| create? | false | - |
|
263
|
+
| read? | false | - |
|
264
|
+
| update? | false | create? |
|
265
|
+
| destroy? | false | create? |
|
266
|
+
| index? | false | read? |
|
267
|
+
| show? | false | read? |
|
268
|
+
| attributes_for_create | auto* | - |
|
269
|
+
| attributes_for_read | auto* | - |
|
270
|
+
| attributes_for_update | auto* | create |
|
271
|
+
| associations | [] | - |
|
272
|
+
|
273
|
+
::: warning
|
274
|
+
\*Auto-detection only works in development
|
275
|
+
:::
|
276
|
+
|
277
|
+
## Security Best Practices
|
278
|
+
|
279
|
+
### 1. Never Skip Authorization
|
280
|
+
|
281
|
+
Plutonium controllers automatically verify authorization:
|
282
|
+
|
283
|
+
```ruby
|
284
|
+
class BlogsController < ResourceController
|
285
|
+
def show
|
286
|
+
# This will raise an error if you forget to authorize
|
287
|
+
# authorize! resource_record
|
288
|
+
render :show
|
289
|
+
end
|
290
|
+
end
|
291
|
+
```
|
292
|
+
|
293
|
+
## Related Resources
|
294
|
+
|
295
|
+
- [Action Policy Documentation](https://actionpolicy.evilmartians.io/)
|
296
|
+
- [Rails Security Guide](https://guides.rubyonrails.org/security.html)
|