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

feat(#8986): Look up a single user from their username #8959

Closed
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
41 changes: 38 additions & 3 deletions api/src/controllers/users.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,9 @@ const logger = require('../logger');
const serverUtils = require('../server-utils');
const replication = require('../services/replication');

const hasFullPermission = req => {
const hasPermissions = (req, permissions) => {
return auth
.check(req, ['can_edit', 'can_update_users'])
.check(req, permissions)
.then(() => true)
.catch(err => {
if (err.code === 403) {
Expand All @@ -19,6 +19,8 @@ const hasFullPermission = req => {
});
};

const hasFullPermission = req => hasPermissions(req, ['can_edit', 'can_update_users']);

const isUpdatingSelf = (req, credentials, username) => {
return auth.getUserCtx(req).then(userCtx => {
return (
Expand Down Expand Up @@ -134,6 +136,35 @@ const convertUserListToV1 = (users=[]) => {
return users;
};

const getUserByUsername = async (req, res) => {
try {
const username = req.params.username;
const credentials = auth.basicAuthCredentials(req);
const [hasPermission, isGettingSelf] = await Promise.all([
hasPermissions(req, 'can_view_users'),
isUpdatingSelf(req, credentials, username),
]).catch(error => {
if (error.statusCode === 401) {
throw { code: 401, message: 'Not logged in', err: error };
}

throw error;
});

if (!hasPermission && !isGettingSelf) {
throw {
message: 'You do not have permissions to fetch this person',
code: 403,
};
}

const body = await users.getUser(username);
res.json(body);
} catch (error) {
serverUtils.error(error, req, res);
}
};

module.exports = {
get: (req, res) => {
return getUserList(req)
Expand Down Expand Up @@ -173,7 +204,7 @@ module.exports = {
]) => {
if (basic === false) {
// If you're passing basic auth we're going to validate it, even if we
// technicaly don't need to (because you already have a valid cookie and
// technically don't need to (because you already have a valid cookie and
// full permission).
// This is to maintain consistency in the personal change password UI:
// we want to validate the password you pass regardless of your permissions
Expand Down Expand Up @@ -236,6 +267,10 @@ module.exports = {

v2: {
get: async (req, res) => {
if (req.params?.username) {
return getUserByUsername(req, res);
}

try {
const body = await getUserList(req);
res.json(body);
Expand Down
1 change: 1 addition & 0 deletions api/src/routing.js
Original file line number Diff line number Diff line change
Expand Up @@ -416,6 +416,7 @@ app.post('/api/v1/forms/validate', textParser, forms.validate);

app.get('/api/v1/users', users.get);
app.get('/api/v2/users', users.v2.get);
app.get('/api/v2/users/:username', users.v2.get);
app.postJson('/api/v1/users', users.create);
app.postJsonOrCsv('/api/v2/users', users.v2.create);
app.postJson('/api/v1/users/:username', users.update);
Expand Down
7 changes: 6 additions & 1 deletion shared-libs/user-management/src/users.js
Original file line number Diff line number Diff line change
Expand Up @@ -768,7 +768,7 @@ const hydrateUserSettings = (userSettings) => {

const getUserDoc = (username, dbName) => {
return db[dbName]
.get(`org.couchdb.user:${username}`)
.get(createID(username))
.catch(err => {
err.db = dbName;
throw err;
Expand Down Expand Up @@ -801,6 +801,11 @@ const getUserSettings = async({ name }) => {
*/
module.exports = {
deleteUser: username => deleteUser(createID(username)),
getUser: async (username) => {
const [user, setting] = await getUserDocsByName(username);
const facilities = await facility.list([user], [setting]);
return mapUsers([user], [setting], facilities)[0];
},
getList: async (filters) => {
const [users, settings] = await getUsersAndSettings(filters);
const facilities = await facility.list(users);
Expand Down
Loading