mirror of
https://github.com/scinote-eln/scinote-web.git
synced 2024-09-21 07:26:15 +08:00
refactor
This commit is contained in:
parent
0d7c116de1
commit
371491c259
|
@ -20,16 +20,16 @@ module ClientApi
|
||||||
.public_send(:find_by_id, @resource.fetch(:id) {
|
.public_send(:find_by_id, @resource.fetch(:id) {
|
||||||
raise ArgumentError, 'ID must be present'
|
raise ArgumentError, 'ID must be present'
|
||||||
}) if @resource
|
}) if @resource
|
||||||
@required_permissions.collect do |permission|
|
@required_permissions.each do |permission|
|
||||||
parsed_permision = permission.gsub('can_', '')
|
trim_permission = permission.gsub('can_', '')
|
||||||
if @resource
|
if @resource
|
||||||
# return false if object does not exist
|
# return false if object does not exist
|
||||||
result = obj ? @holder.eval('read_team', current_user, obj) : false
|
result = obj ? @holder.eval(trim_permission, current_user, obj) : false
|
||||||
@permissions.merge!(permission => result)
|
@permissions.merge!(permission => result)
|
||||||
else
|
else
|
||||||
@permissions.merge!(
|
@permissions.merge!(
|
||||||
permission => @holder.eval_generic(
|
permission => @holder.eval_generic(
|
||||||
parsed_permision, current_user
|
trim_permission, current_user
|
||||||
)
|
)
|
||||||
)
|
)
|
||||||
end
|
end
|
||||||
|
|
|
@ -8,20 +8,20 @@
|
||||||
pass this "permissions helper methods" in your component params:
|
pass this "permissions helper methods" in your component params:
|
||||||
|
|
||||||
If you need to specific model you have to specify it in the connect method
|
If you need to specific model you have to specify it in the connect method
|
||||||
like the example below
|
like the example below:
|
||||||
>
|
>
|
||||||
> Permissions.connect(MyComponent, ["can_update_team", "can_read_team"], "Team");
|
> Permissions.connect(MyComponent, ["can_update_team", "can_read_team"], "Team");
|
||||||
>
|
>
|
||||||
|
|
||||||
In case your component is connected to Redux or some other HOC you can simply
|
In case your component is connected to Redux or some other HOC you can simply
|
||||||
chain the HOC's
|
chain the HOC's:
|
||||||
>
|
>
|
||||||
> const mapStateToProps = ({ current_team }) => ({ current_team });
|
> const mapStateToProps = ({ current_team }) => ({ current_team });
|
||||||
> const MyComponentWithPermissions = Permissions.connect(MyComponent, ["can_read_team"], "Team");
|
> const MyComponentWithPermissions = Permissions.connect(MyComponent, ["can_read_team"], "Team");
|
||||||
> export default connect(mapStateToProps)(MyComponentWithPermissions);
|
> export default connect(mapStateToProps)(MyComponentWithPermissions);
|
||||||
>
|
>
|
||||||
beside the permissions object you get the setPermissionResourceId/1 function
|
Beside the permissions object there's also the setPermissionResourceId/1 function in your component props.
|
||||||
in your component props. Than you can pass the id of the resource when you have it.
|
This function should be used to pass the ID of the resource when you have it.
|
||||||
|
|
||||||
Example: you need the current team for whatever reason you can call it in the
|
Example: you need the current team for whatever reason you can call it in the
|
||||||
shouldComponentUpdate function...
|
shouldComponentUpdate function...
|
||||||
|
@ -31,11 +31,10 @@
|
||||||
> return true; // remember to return true!
|
> return true; // remember to return true!
|
||||||
> }
|
> }
|
||||||
|
|
||||||
JUST REMEMBER THAT YOU HAVE PASS A VALID ID and you have to check if is present
|
JUST REMEMBER THAT YOU HAVE TO PASS A VALID ID and you have to check if it's present at the moment of the execution.
|
||||||
at the moment of execution. THE REQUEST WILL BE TRIGGERED WHEN YOU INVOKE
|
THE REQUEST WILL BE TRIGGERED WHEN YOU INVOKE setPermissionResourceId/1 FUNCTION!!!
|
||||||
setPermissionResourceId/1 FUNCTION!!!!
|
|
||||||
|
|
||||||
else you can pass just the perrmissions on the user
|
Otherwise (generic permissions without the accessed object) you can just pass the permissions on the user
|
||||||
>
|
>
|
||||||
> Permissions.connect(MyComponent, ["can_update"])
|
> Permissions.connect(MyComponent, ["can_update"])
|
||||||
>
|
>
|
||||||
|
@ -44,9 +43,8 @@
|
||||||
Now you can access your permissions through component params. The permissions
|
Now you can access your permissions through component params. The permissions
|
||||||
you required have 3 states [true, false, null]. Null is when you are waiting for server response.
|
you required have 3 states [true, false, null]. Null is when you are waiting for server response.
|
||||||
|
|
||||||
Finally you can access to your permissions in the component using
|
Finally you can access your permissions in the component using props.permissions.can_... whatever you specify.
|
||||||
props.permissions.can_.... whatever you specify. The props.permissions is
|
The props.permissions is basically an object that holds all required permissions.
|
||||||
basically an object that holds all required permissions.
|
|
||||||
*/
|
*/
|
||||||
|
|
||||||
import * as React from "react";
|
import * as React from "react";
|
||||||
|
|
Loading…
Reference in a new issue