Skip to content

Commit 171b21e

Browse files
committed
Add permissions handling to the console; remove kill permission (wrapped in with stop)
1 parent 79095b5 commit 171b21e

File tree

5 files changed

+42
-28
lines changed

5 files changed

+42
-28
lines changed

app/Http/Requests/Api/Client/Servers/SendPowerRequest.php

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,11 +18,10 @@ public function permission(): string
1818
case 'start':
1919
return Permission::ACTION_CONTROL_START;
2020
case 'stop':
21+
case 'kill':
2122
return Permission::ACTION_CONTROL_STOP;
2223
case 'restart':
2324
return Permission::ACTION_CONTROL_RESTART;
24-
case 'kill':
25-
return Permission::ACTION_CONTROL_KILL;
2625
}
2726

2827
return '__invalid';

app/Models/Permission.php

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,6 @@ class Permission extends Validable
2020
const ACTION_CONTROL_START = 'control.start';
2121
const ACTION_CONTROL_STOP = 'control.stop';
2222
const ACTION_CONTROL_RESTART = 'control.restart';
23-
const ACTION_CONTROL_KILL = 'control.kill';
2423

2524
const ACTION_DATABASE_READ = 'database.read';
2625
const ACTION_DATABASE_CREATE = 'database.create';
@@ -111,7 +110,6 @@ class Permission extends Validable
111110
'start' => 'Allows a user to start the server if it is stopped.',
112111
'stop' => 'Allows a user to stop a server if it is running.',
113112
'restart' => 'Allows a user to perform a server restart. This allows them to start the server if it is offline, but not put the server in a completely stopped state.',
114-
'kill' => 'Allows a user to terminate a server process.',
115113
],
116114
],
117115

resources/scripts/components/server/Console.tsx

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,9 @@ import * as TerminalFit from 'xterm/lib/addons/fit/fit';
44
import SpinnerOverlay from '@/components/elements/SpinnerOverlay';
55
import { ServerContext } from '@/state/server';
66
import styled from 'styled-components';
7+
import Can from '@/components/elements/Can';
8+
import { usePermissions } from '@/plugins/usePermissions';
9+
import classNames from 'classnames';
710

811
const theme = {
912
background: 'transparent',
@@ -52,6 +55,7 @@ export default () => {
5255
const useRef = useCallback(node => setTerminalElement(node), []);
5356
const terminal = useMemo(() => new Terminal({ ...terminalProps }), []);
5457
const { connected, instance } = ServerContext.useStoreState(state => state.socket);
58+
const [ canSendCommands ] = usePermissions([ 'control.console']);
5559

5660
const handleConsoleOutput = (line: string, prelude = false) => terminal.writeln(
5761
(prelude ? TERMINAL_PRELUDE : '') + line.replace(/(?:\r\n|\r|\n)$/im, '') + '\u001b[0m',
@@ -121,14 +125,17 @@ export default () => {
121125
<div className={'text-xs font-mono relative'}>
122126
<SpinnerOverlay visible={!connected} size={'large'}/>
123127
<div
124-
className={'rounded-t p-2 bg-black w-full'}
128+
className={classNames('rounded-t p-2 bg-black w-full', {
129+
'rounded-b': !canSendCommands,
130+
})}
125131
style={{
126132
minHeight: '16rem',
127133
maxHeight: '32rem',
128134
}}
129135
>
130136
<TerminalDiv id={'terminal'} ref={useRef}/>
131137
</div>
138+
{canSendCommands &&
132139
<div className={'rounded-b bg-neutral-900 text-neutral-100 flex'}>
133140
<div className={'flex-no-shrink p-2 font-bold'}>$</div>
134141
<div className={'w-full'}>
@@ -140,6 +147,7 @@ export default () => {
140147
/>
141148
</div>
142149
</div>
150+
}
143151
</div>
144152
);
145153
};

resources/scripts/components/server/ServerConsole.tsx

Lines changed: 31 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ import { faMicrochip } from '@fortawesome/free-solid-svg-icons/faMicrochip';
99
import { bytesToHuman } from '@/helpers';
1010
import SuspenseSpinner from '@/components/elements/SuspenseSpinner';
1111
import TitledGreyBox from '@/components/elements/TitledGreyBox';
12+
import Can from '@/components/elements/Can';
1213

1314
type PowerAction = 'start' | 'stop' | 'restart' | 'kill';
1415

@@ -109,28 +110,36 @@ export default () => {
109110
&nbsp;{cpu.toFixed(2)} %
110111
</p>
111112
</TitledGreyBox>
112-
<div className={'grey-box justify-center'}>
113-
<button
114-
className={'btn btn-secondary btn-xs mr-2'}
115-
disabled={status !== 'offline'}
116-
onClick={e => {
117-
e.preventDefault();
118-
sendPowerCommand('start');
119-
}}
120-
>
121-
Start
122-
</button>
123-
<button
124-
className={'btn btn-secondary btn-xs mr-2'}
125-
onClick={e => {
126-
e.preventDefault();
127-
sendPowerCommand('restart');
128-
}}
129-
>
130-
Restart
131-
</button>
132-
<StopOrKillButton onPress={action => sendPowerCommand(action)}/>
133-
</div>
113+
<Can action={[ 'control.start', 'control.stop', 'control.restart' ]} matchAny={true}>
114+
<div className={'grey-box justify-center'}>
115+
<Can action={'control.start'}>
116+
<button
117+
className={'btn btn-secondary btn-xs mr-2'}
118+
disabled={status !== 'offline'}
119+
onClick={e => {
120+
e.preventDefault();
121+
sendPowerCommand('start');
122+
}}
123+
>
124+
Start
125+
</button>
126+
</Can>
127+
<Can action={'control.restart'}>
128+
<button
129+
className={'btn btn-secondary btn-xs mr-2'}
130+
onClick={e => {
131+
e.preventDefault();
132+
sendPowerCommand('restart');
133+
}}
134+
>
135+
Restart
136+
</button>
137+
</Can>
138+
<Can action={'control.stop'}>
139+
<StopOrKillButton onPress={action => sendPowerCommand(action)}/>
140+
</Can>
141+
</div>
142+
</Can>
134143
</div>
135144
<div className={'flex-1 mx-4 mr-4'}>
136145
<SuspenseSpinner>

resources/scripts/state/server/subusers.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ import { action, Action } from 'easy-peasy';
22

33
export type SubuserPermission =
44
'websocket.*' |
5-
'control.console' | 'control.start' | 'control.stop' | 'control.restart' | 'control.kill' |
5+
'control.console' | 'control.start' | 'control.stop' | 'control.restart' |
66
'user.create' | 'user.read' | 'user.update' | 'user.delete' |
77
'file.create' | 'file.read' | 'file.update' | 'file.delete' | 'file.archive' | 'file.sftp' |
88
'allocation.read' | 'allocation.update' |

0 commit comments

Comments
 (0)