mirror of
https://github.com/scinote-eln/scinote-web.git
synced 2025-10-29 15:36:36 +08:00
fixes per @duco 's request
This commit is contained in:
parent
52e5cf7be8
commit
b106545d31
4 changed files with 13 additions and 10 deletions
|
|
@ -2,7 +2,7 @@ module ClientApi
|
||||||
class PermissionsController < ApplicationController
|
class PermissionsController < ApplicationController
|
||||||
before_action :generate_permissions_object, only: :state
|
before_action :generate_permissions_object, only: :state
|
||||||
|
|
||||||
def state
|
def status
|
||||||
respond_to do |format|
|
respond_to do |format|
|
||||||
format.json do
|
format.json do
|
||||||
render json: @permissions, status: :ok
|
render json: @permissions, status: :ok
|
||||||
|
|
|
||||||
|
|
@ -11,9 +11,9 @@
|
||||||
> Permissions.connect(MyComponent, ["can_update_team?", "can_read_team?"], "Team");
|
> Permissions.connect(MyComponent, ["can_update_team?", "can_read_team?"], "Team");
|
||||||
>
|
>
|
||||||
|
|
||||||
Now you can access to 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.
|
||||||
You can use methods params.can_uspdate_team? or whatever permissions you declare
|
You can use methods params.can_update_team? or whatever permissions you declare
|
||||||
*/
|
*/
|
||||||
import * as React from "react";
|
import * as React from "react";
|
||||||
import { getPermissionStatus } from "../api/permissions_api";
|
import { getPermissionStatus } from "../api/permissions_api";
|
||||||
|
|
@ -22,6 +22,9 @@ type State = {
|
||||||
permissions: any
|
permissions: any
|
||||||
};
|
};
|
||||||
|
|
||||||
|
type PermissionsObject = {
|
||||||
|
[string]: boolean
|
||||||
|
}
|
||||||
/*
|
/*
|
||||||
This function accepts 3 arguments which are REQUIRED
|
This function accepts 3 arguments which are REQUIRED
|
||||||
1.) WrappedComponent: Component that you want to have permissions
|
1.) WrappedComponent: Component that you want to have permissions
|
||||||
|
|
@ -34,15 +37,15 @@ export function connect<Props: {}>(
|
||||||
requiredPermissions: Array<string> = [],
|
requiredPermissions: Array<string> = [],
|
||||||
resource: string
|
resource: string
|
||||||
) {
|
) {
|
||||||
let parsedPermission = {};
|
const parsedPermissions: PermissionsObject = {};
|
||||||
requiredPermissions.forEach(el => {
|
requiredPermissions.forEach(el => {
|
||||||
parsedPermission[el] = null;
|
parsedPermissions[el] = null;
|
||||||
});
|
});
|
||||||
|
|
||||||
return class extends React.Component<*, State, *> {
|
return class extends React.Component<*, State, *> {
|
||||||
constructor(props: any) {
|
constructor(props: any) {
|
||||||
super(props);
|
super(props);
|
||||||
this.state = { permissions: parsedPermission };
|
this.state = { permissions: parsedPermissions };
|
||||||
}
|
}
|
||||||
|
|
||||||
componentDidMount(): void {
|
componentDidMount(): void {
|
||||||
|
|
@ -51,7 +54,7 @@ export function connect<Props: {}>(
|
||||||
this.setState({ permissions: data });
|
this.setState({ permissions: data });
|
||||||
})
|
})
|
||||||
.catch(() => {
|
.catch(() => {
|
||||||
let permissions = {};
|
const permissions: PermissionsObject = {};
|
||||||
requiredPermissions.forEach(el => {
|
requiredPermissions.forEach(el => {
|
||||||
permissions[el] = false;
|
permissions[el] = false;
|
||||||
});
|
});
|
||||||
|
|
|
||||||
|
|
@ -18,7 +18,7 @@ Rails.application.routes.draw do
|
||||||
get '/settings/*all', to: 'client_api/settings#index'
|
get '/settings/*all', to: 'client_api/settings#index'
|
||||||
|
|
||||||
namespace :client_api, defaults: { format: 'json' } do
|
namespace :client_api, defaults: { format: 'json' } do
|
||||||
post '/premissions', to: 'permissions#state'
|
post '/premissions', to: 'permissions#status'
|
||||||
%i(activities teams notifications users configurations).each do |path|
|
%i(activities teams notifications users configurations).each do |path|
|
||||||
draw path
|
draw path
|
||||||
end
|
end
|
||||||
|
|
|
||||||
|
|
@ -3,11 +3,11 @@ require 'rails_helper'
|
||||||
describe ClientApi::PermissionsController, type: :controller do
|
describe ClientApi::PermissionsController, type: :controller do
|
||||||
login_user
|
login_user
|
||||||
|
|
||||||
describe '#state' do
|
describe '#status' do
|
||||||
let(:params) do
|
let(:params) do
|
||||||
{ parsePermission: ['can_view_team'], resource: 'UserTeam' }
|
{ parsePermission: ['can_view_team'], resource: 'UserTeam' }
|
||||||
end
|
end
|
||||||
let(:subject) { post :state, format: :json, params: params }
|
let(:subject) { post :status, format: :json, params: params }
|
||||||
it { is_expected.to be_success }
|
it { is_expected.to be_success }
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
|
||||||
Loading…
Add table
Reference in a new issue