Skip to content
Projects
Groups
Snippets
Help
This project
Loading...
Sign in / Register
Toggle navigation
S
simple_controller
Project
Overview
Details
Activity
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
open-source
simple_controller
Commits
105ab79f
Commit
105ab79f
authored
Jun 24, 2021
by
liyijie
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
add authorize_if_policy_class method
parent
da1db75e
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
22 additions
and
6 deletions
+22
-6
base_controller.rb
lib/simple_controller/base_controller.rb
+22
-6
No files found.
lib/simple_controller/base_controller.rb
View file @
105ab79f
...
@@ -124,19 +124,26 @@ class SimpleController::BaseController < ::InheritedResources::Base
...
@@ -124,19 +124,26 @@ class SimpleController::BaseController < ::InheritedResources::Base
# 对于resource的相关操作,都调用policy进行authorize
# 对于resource的相关操作,都调用policy进行authorize
def
set_resource_ivar
(
resource
)
def
set_resource_ivar
(
resource
)
policy_class
=
self
.
class
.
instance_variable_get
(
:@policy_class
)
_resource
=
authorize_if_policy_class
resource
,
"
#{
action_name
}
?"
_resource
=
policy_class
&
.
method_defined?
(
"
#{
action_name
}
?"
)
?
authorize
(
resource
,
policy_class:
policy_class
)
:
resource
instance_variable_set
(
"@
#{
resource_instance_name
}
"
,
_resource
)
instance_variable_set
(
"@
#{
resource_instance_name
}
"
,
_resource
)
end
end
def
set_collection_ivar
(
collection
)
def
set_collection_ivar
(
collection
)
policy_class
=
self
.
class
.
instance_variable_get
(
:@policy_class
)
authorize_if_policy_class
resource_class
,
"
#{
action_name
}
?"
authorize
(
resource_class
,
policy_class:
policy_class
)
if
policy_class
&
.
method_defined?
(
"
#{
action_name
}
?"
)
instance_variable_set
(
"@
#{
resource_collection_name
}
"
,
collection
)
instance_variable_set
(
"@
#{
resource_collection_name
}
"
,
collection
)
end
end
def
association_chain
@association_chain
||=
symbols_for_association_chain
.
inject
([
begin_of_association_chain
])
do
|
chain
,
symbol
|
parent_instance
=
evaluate_parent
(
symbol
,
resources_configuration
[
symbol
],
chain
.
last
)
# policy parent
parent_config
=
resources_configuration
[
symbol
]
authorize_if_policy_class
parent_instance
,
"parent_
#{
parent_config
[
:instance_name
]
}
?"
chain
<<
parent_instance
end
.
compact
.
freeze
end
def
view_path
def
view_path
self
.
class
.
instance_variable_get
(
:@view_path
)
||
self
.
class
.
instance_variable_get
(
:@view_path
)
||
self
.
class
.
instance_variable_set
(
:@view_path
,
extract_view_path
)
self
.
class
.
instance_variable_set
(
:@view_path
,
extract_view_path
)
...
@@ -185,4 +192,13 @@ class SimpleController::BaseController < ::InheritedResources::Base
...
@@ -185,4 +192,13 @@ class SimpleController::BaseController < ::InheritedResources::Base
raise
raise
end
end
end
end
private
def
authorize_if_policy_class
record
,
query
,
policy_class:
nil
policy_class
||=
self
.
class
.
instance_variable_get
(
:@policy_class
)
policy_class
&
.
method_defined?
(
query
)
?
authorize
(
record
,
query
,
policy_class:
policy_class
)
:
record
end
end
end
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment