• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

mendersoftware / gui / 1315689429

03 Jun 2024 09:57AM UTC coverage: 83.446% (-16.5%) from 99.964%
1315689429

Pull #4427

gitlab-ci

mzedel
feat: allowed adding users by user id in user creation dialog

Ticket: MEN-7277
Changelog: Title
Signed-off-by: Manuel Zedel <manuel.zedel@northern.tech>
Pull Request #4427: MEN-7083 - show user id & allow adding user by id to tenant

4485 of 6406 branches covered (70.01%)

38 of 41 new or added lines in 8 files covered. (92.68%)

1670 existing lines in 162 files now uncovered.

8509 of 10197 relevant lines covered (83.45%)

140.48 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

85.71
/src/js/components/settings/user-management/usermanagement.js
1
// Copyright 2017 Northern.tech AS
2
//
3
//    Licensed under the Apache License, Version 2.0 (the "License");
4
//    you may not use this file except in compliance with the License.
5
//    You may obtain a copy of the License at
6
//
7
//        http://www.apache.org/licenses/LICENSE-2.0
8
//
9
//    Unless required by applicable law or agreed to in writing, software
10
//    distributed under the License is distributed on an "AS IS" BASIS,
11
//    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12
//    See the License for the specific language governing permissions and
13
//    limitations under the License.
14
import React, { useEffect, useState } from 'react';
15
import { useDispatch, useSelector } from 'react-redux';
16

17
// material ui
18
import { Button, Dialog, DialogActions, DialogContent, DialogTitle } from '@mui/material';
19

20
import { setSnackbar } from '../../../actions/appActions';
21
import { addUserToCurrentTenant, createUser, editUser, getUserList, passwordResetStart, removeUser } from '../../../actions/userActions';
22
import { getCurrentUser, getFeatures, getIsEnterprise, getRolesById, getUserCapabilities } from '../../../selectors';
23
import { UserDefinition } from './userdefinition';
24
import UserForm from './userform';
25
import UserList from './userlist';
26

27
const actions = {
4✔
28
  add: 'addUser',
29
  create: 'createUser',
30
  edit: 'editUser',
31
  remove: 'removeUser'
32
};
33

34
const DeleteUserDialog = ({ dismiss, open, submit, user }) => (
4✔
35
  <Dialog open={open}>
20✔
36
    <DialogTitle>Delete user?</DialogTitle>
37
    <DialogContent style={{ overflow: 'hidden' }}>
38
      Are you sure you want to delete the user with email{' '}
39
      <b>
40
        <i>{user.email}</i>
41
      </b>
42
      ?
43
    </DialogContent>
44
    <DialogActions>
45
      <Button style={{ marginRight: 10 }} onClick={dismiss}>
46
        Cancel
47
      </Button>
UNCOV
48
      <Button variant="contained" color="primary" onClick={() => submit(user, 'remove', user.id)}>
×
49
        Delete user
50
      </Button>
51
    </DialogActions>
52
  </Dialog>
53
);
54

55
export const UserManagement = () => {
4✔
56
  const [showCreate, setShowCreate] = useState(false);
20✔
57
  const [removeDialog, setRemoveDialog] = useState(false);
20✔
58
  const [user, setUser] = useState({});
20✔
59
  const dispatch = useDispatch();
20✔
60

61
  const { canManageUsers } = useSelector(getUserCapabilities);
20✔
62
  const { isHosted } = useSelector(getFeatures);
20✔
63
  const isEnterprise = useSelector(getIsEnterprise);
20✔
64
  const currentUser = useSelector(getCurrentUser);
20✔
65
  const roles = useSelector(getRolesById);
20✔
66
  const users = useSelector(state => Object.values(state.users.byId));
43✔
67
  const props = {
20✔
68
    canManageUsers,
NEW
69
    addUser: (id, tenantId) => dispatch(addUserToCurrentTenant(id, tenantId)),
×
70
    createUser: userData => dispatch(createUser(userData)),
2✔
71
    currentUser,
72
    editUser: (id, userData) => dispatch(editUser(id, userData)),
1✔
73
    isEnterprise,
74
    isHosted,
UNCOV
75
    removeUser: id => dispatch(removeUser(id)),
×
76
    roles,
77
    users
78
  };
79

80
  useEffect(() => {
20✔
81
    dispatch(getUserList());
5✔
82
  }, [dispatch]);
83

84
  useEffect(() => {
20✔
85
    dispatch(getUserList());
5✔
86
  }, [currentUser.id, dispatch, users.length]);
87

88
  const openEdit = user => {
20✔
89
    setUser(user);
3✔
90
    setRemoveDialog(false);
3✔
91
    dispatch(setSnackbar(''));
3✔
92
  };
93

94
  const openRemove = () => {
20✔
95
    dispatch(setSnackbar(''));
1✔
96
    setRemoveDialog(true);
1✔
97
  };
98

99
  const dialogDismiss = () => {
20✔
100
    setUser({});
3✔
101
    setShowCreate(false);
3✔
102
    setRemoveDialog(false);
3✔
103
  };
104

105
  const submit = (userData, type, id, passwordResetEmail) => {
20✔
106
    if (userData) {
3!
107
      let request = null;
3✔
108
      if (id) {
3✔
109
        request = props[actions[type]](id, userData);
1✔
110
      } else {
111
        request = props[actions[type]](userData);
2✔
112
      }
113
      return request.then(() => {
3✔
114
        if (passwordResetEmail) {
2!
UNCOV
115
          dispatch(passwordResetStart(passwordResetEmail));
×
116
        }
117
        dialogDismiss();
2✔
118
      });
119
    }
UNCOV
120
    if (passwordResetEmail) {
×
UNCOV
121
      dispatch(passwordResetStart(passwordResetEmail));
×
122
    }
UNCOV
123
    return dialogDismiss();
×
124
  };
125

126
  return (
20✔
127
    <div>
128
      <div className="flexbox centered space-between" style={{ marginLeft: '20px' }}>
129
        <h2>Users</h2>
130
        <Button variant="contained" color="primary" onClick={setShowCreate}>
131
          Add new user
132
        </Button>
133
      </div>
134

135
      <UserList {...props} editUser={openEdit} />
136
      {showCreate && <UserForm {...props} closeDialog={dialogDismiss} submit={submit} />}
26✔
137
      <UserDefinition
138
        currentUser={currentUser}
139
        isEnterprise={isEnterprise}
140
        onRemove={openRemove}
141
        onCancel={dialogDismiss}
142
        onSubmit={submit}
143
        roles={roles}
144
        selectedUser={user}
145
      />
146
      <DeleteUserDialog dismiss={dialogDismiss} open={removeDialog} submit={submit} user={user} />
147
    </div>
148
  );
149
};
150

151
export default UserManagement;
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2025 Coveralls, Inc