Skip to content

Commit

Permalink
fix: flag for anonymising user search
Browse files Browse the repository at this point in the history
  • Loading branch information
ivarconr committed May 31, 2022
1 parent 476fa79 commit efeff14
Showing 1 changed file with 19 additions and 2 deletions.
21 changes: 19 additions & 2 deletions src/lib/routes/admin-api/user-admin.ts
Expand Up @@ -11,8 +11,9 @@ import { IUnleashServices } from '../../types/services';
import SessionService from '../../services/session-service';
import { IAuthRequest } from '../unleash-types';
import SettingService from '../../services/setting-service';
import { SimpleAuthSettings } from '../../server-impl';
import { IUser, SimpleAuthSettings } from '../../server-impl';
import { simpleAuthKey } from '../../types/settings/simple-auth-settings';
import { anonymise } from '../../util/anonymise';

interface ICreateUserBody {
username: string;
Expand All @@ -23,6 +24,8 @@ interface ICreateUserBody {
}

export default class UserAdminController extends Controller {
private anonymise: boolean = false;

private userService: UserService;

private accessService: AccessService;
Expand Down Expand Up @@ -67,6 +70,7 @@ export default class UserAdminController extends Controller {
this.settingService = settingService;
this.logger = config.getLogger('routes/user-controller.ts');
this.unleashUrl = config.server.unleashUrl;
this.anonymise = config.experimental?.anonymiseEventLog;

this.get('/', this.getUsers, ADMIN);
this.get('/search', this.search);
Expand Down Expand Up @@ -106,11 +110,24 @@ export default class UserAdminController extends Controller {
res.json(sessions);
}

anonymiseUsers(users: IUser[]): IUser[] {
return users.map((u) => ({
...u,
email: anonymise(u.email || 'random'),
imageUrl:
'https://gravatar.com/avatar/21232f297a57a5a743894a0e4a801fc3?size=42&default=retro',
}));
}

async search(req: Request, res: Response): Promise<void> {
const { q } = req.query as any;
try {
const users =
let users =
q && q.length > 1 ? await this.userService.search(q) : [];

if (this.anonymise) {
users = this.anonymiseUsers(users);
}
res.json(users);
} catch (error) {
this.logger.error(error);
Expand Down

0 comments on commit efeff14

Please sign in to comment.