Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feature/impersonate #119

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 19 additions & 0 deletions device/create.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,25 @@ func (r *ResolverForDevice) Login(ctx context.Context, username string, pass str
return r.createDeviceInternal(ctx, user, deviceName, deviceType, cookie)
}


// Impersonate creates a device.
func (r *ResolverForDevice) Impersonate(ctx context.Context, username string, deviceName string, deviceType gqlmodel.DeviceType, cookie bool) (*gqlmodel.Login, error) {

current_user := auth.GetUser(ctx)

if current_user.Admin == false {
return nil, errors.New("needs to be admin to impersonate")
}

user := new(model.User)
find := r.DB.Where("name = ?", username).Find(user)
if find.RecordNotFound() {
return nil, errors.New("username does not exist")
}

return r.createDeviceInternal(ctx, user, deviceName, deviceType, cookie)
}

// CreateDevice creates a device.
func (r *ResolverForDevice) CreateDevice(ctx context.Context, deviceName string, deviceType gqlmodel.DeviceType) (*gqlmodel.Login, error) {

Expand Down
1 change: 1 addition & 0 deletions schema.graphql
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ type RootMutation {
updateUser(id: Int!, name: String!, pass: String, admin: Boolean!): User @hasRole(role: ADMIN)

login(username: String!, pass: String!, deviceName: String!, type: DeviceType!, cookie: Boolean!): Login
impersonate(username: String!, deviceName: String!, type: DeviceType!, cookie: Boolean!): Login @hasRole(role: ADMIN)

createDevice(name: String!, type: DeviceType!): Login @hasRole(role: USER)
updateDevice(id: Int!, name: String!, type: DeviceType!): Device
Expand Down
12 changes: 12 additions & 0 deletions ui/src/gql/user.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,18 @@ export const Logout = gql`
}
`;

export const Impersonate = gql`
mutation Impersonate($name: String!, $deviceType: DeviceType!) {
impersonate(username: $name, deviceName: "web ui (impersonation)", type: $deviceType, cookie: true) {
user {
id
name
admin
}
}
}
`;

export const Users = gql`
query Users {
users {
Expand Down
23 changes: 22 additions & 1 deletion ui/src/user/UsersPage.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import TableHead from '@material-ui/core/TableHead';
import TableRow from '@material-ui/core/TableRow';
import DeleteIcon from '@material-ui/icons/Delete';
import EditIcon from '@material-ui/icons/Edit';
import CameraFrontIcon from '@material-ui/icons/CameraFront';
import DoneIcon from '@material-ui/icons/Done';
import CloseIcon from '@material-ui/icons/Close';
import IconButton from '@material-ui/core/IconButton';
Expand All @@ -18,8 +19,10 @@ import {TextField} from '@material-ui/core';
import {Users} from '../gql/__generated__/Users';
import {RemoveUser, RemoveUserVariables} from '../gql/__generated__/RemoveUser';
import {UpdateUser, UpdateUserVariables} from '../gql/__generated__/UpdateUser';
import {Impersonate, ImpersonateVariables} from '../gql/__generated__/Impersonate';
import Checkbox from '@material-ui/core/Checkbox';
import Button from '@material-ui/core/Button';
import {DeviceType} from '../gql/__generated__/globalTypes';
import {AddUserDialog} from './AddUserDialog';
import makeStyles from '@material-ui/core/styles/makeStyles';
import {ConfirmDialog} from '../common/ConfirmDialog';
Expand Down Expand Up @@ -50,6 +53,11 @@ export const UsersPage = () => {
NoEdit
);
const [updateUser] = useMutation<UpdateUser, UpdateUserVariables>(gqlUser.UpdateUser, refetch);
const [impersonate] = useMutation<Impersonate, ImpersonateVariables>(gqlUser.Impersonate, {
update: (cache, {data}) => {
cache.writeQuery({query: gqlUser.CurrentUser, data: {user: data && data.impersonate && data.impersonate.user}});
},
});
if (loading || !data || !data.currentUser || !data.users) {
return <CenteredSpinner />;
}
Expand All @@ -73,6 +81,16 @@ export const UsersPage = () => {
},
}).then(() => enqueueSnackbar('user edited', {variant: 'success'}));
};
const impersonateClickHandler = (username: string) => {
impersonate({
variables: {
name: username,
deviceType: DeviceType.ShortExpiry,
}
})
.then(() => enqueueSnackbar('Impersonation successful', {variant: 'success'}))
.catch(() => enqueueSnackbar('Impersonation failed'));
};
const isCurrent = user.id === data.currentUser!.id;
const isEdited = editId === user.id;
return (
Expand Down Expand Up @@ -127,6 +145,9 @@ export const UsersPage = () => {
</>
) : (
<>
<IconButton onClick={() => impersonateClickHandler(user.name)} title="Impersonate">
<CameraFrontIcon />
</IconButton>
<IconButton onClick={() => setEditing([user.id, user.name, '', user.admin])} title="Edit">
<EditIcon />
</IconButton>
Expand Down Expand Up @@ -159,7 +180,7 @@ export const UsersPage = () => {
<TableCell>Name</TableCell>
<TableCell>Password</TableCell>
<TableCell>Admin</TableCell>
<TableCell style={{width: 150}} />
<TableCell style={{width: 200}} />
</TableRow>
</TableHead>
<TableBody>{users}</TableBody>
Expand Down