Skip to content

Commit

Permalink
fix: remove configurable profile permission settings
Browse files Browse the repository at this point in the history
  • Loading branch information
I-vasilich-I committed Nov 22, 2023
1 parent 2e47e0a commit ea2b701
Show file tree
Hide file tree
Showing 4 changed files with 45 additions and 346 deletions.
159 changes: 1 addition & 158 deletions server/src/routes/profile/__test__/permissions.test.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { CourseRole, IUserSession } from '../../../models';
import { getPermissions, defineRole, getProfilePermissionsSettings } from '../permissions';
import { getPermissions, defineRole } from '../permissions';

const mockSession = {
id: 1,
Expand Down Expand Up @@ -38,17 +38,6 @@ describe('getPermissions', () => {
}),
).toEqual({
isProfileVisible: false,
isAboutVisible: false,
isEducationVisible: false,
isEnglishVisible: false,
isEmailVisible: false,
isTelegramVisible: false,
isWhatsAppVisible: false,
isSkypeVisible: false,
isPhoneVisible: false,
isContactsNotesVisible: false,
isLinkedInVisible: false,
isPublicFeedbackVisible: false,
isMentorStatsVisible: false,
isStudentStatsVisible: false,
isStageInterviewFeedbackVisible: false,
Expand All @@ -68,33 +57,12 @@ describe('getPermissions', () => {
role: 'all',
permissions: {
isProfileVisible: { all: true },
isAboutVisible: { all: true, mentor: true, student: true },
isEducationVisible: { all: true, mentor: true, student: true },
isEnglishVisible: { all: false, student: false },
isEmailVisible: { all: true, student: true },
isTelegramVisible: { all: false, student: false },
isSkypeVisible: { all: true, student: true },
isPhoneVisible: { all: false, student: false },
isContactsNotesVisible: { all: true, student: true },
isLinkedInVisible: { all: false, mentor: false, student: false },
isPublicFeedbackVisible: { all: true, mentor: true, student: true },
isMentorStatsVisible: { all: true, mentor: true, student: true },
isStudentStatsVisible: { all: true, student: true },
},
}),
).toEqual({
isProfileVisible: true,
isAboutVisible: true,
isEducationVisible: true,
isEnglishVisible: false,
isEmailVisible: true,
isTelegramVisible: false,
isWhatsAppVisible: false,
isSkypeVisible: true,
isPhoneVisible: false,
isContactsNotesVisible: true,
isLinkedInVisible: false,
isPublicFeedbackVisible: true,
isMentorStatsVisible: true,
isStudentStatsVisible: true,
isStageInterviewFeedbackVisible: false,
Expand All @@ -111,33 +79,12 @@ describe('getPermissions', () => {
role: 'mentor',
permissions: {
isProfileVisible: { all: true },
isAboutVisible: { all: false, mentor: true, student: false },
isEducationVisible: { all: false, mentor: false, student: true },
isEnglishVisible: { all: false, student: false },
isEmailVisible: { all: false, student: true },
isTelegramVisible: { all: false, student: false },
isSkypeVisible: { all: false, student: true },
isPhoneVisible: { all: false, student: false },
isContactsNotesVisible: { all: true, student: true },
isLinkedInVisible: { all: false, mentor: false, student: false },
isPublicFeedbackVisible: { all: false, mentor: true, student: true },
isMentorStatsVisible: { all: false, mentor: true, student: true },
isStudentStatsVisible: { all: false, student: true },
},
}),
).toEqual({
isProfileVisible: true,
isAboutVisible: true,
isEducationVisible: false,
isEnglishVisible: true,
isEmailVisible: true,
isTelegramVisible: true,
isWhatsAppVisible: true,
isSkypeVisible: true,
isPhoneVisible: true,
isContactsNotesVisible: true,
isLinkedInVisible: false,
isPublicFeedbackVisible: true,
isMentorStatsVisible: true,
isStudentStatsVisible: true,
isStageInterviewFeedbackVisible: true,
Expand All @@ -154,33 +101,12 @@ describe('getPermissions', () => {
role: 'student',
permissions: {
isProfileVisible: { all: true },
isAboutVisible: { all: false, mentor: true, student: true },
isEducationVisible: { all: false, mentor: false, student: false },
isEnglishVisible: { all: false, student: false },
isEmailVisible: { all: false, student: false },
isTelegramVisible: { all: false, student: true },
isSkypeVisible: { all: false, student: true },
isPhoneVisible: { all: false, student: false },
isContactsNotesVisible: { all: true, student: true },
isLinkedInVisible: { all: false, mentor: false, student: false },
isPublicFeedbackVisible: { all: false, mentor: true, student: true },
isMentorStatsVisible: { all: false, mentor: true, student: true },
isStudentStatsVisible: { all: false, student: true },
},
}),
).toEqual({
isProfileVisible: true,
isAboutVisible: true,
isEducationVisible: false,
isEnglishVisible: false,
isEmailVisible: false,
isTelegramVisible: true,
isWhatsAppVisible: true,
isSkypeVisible: true,
isPhoneVisible: false,
isContactsNotesVisible: true,
isLinkedInVisible: false,
isPublicFeedbackVisible: true,
isMentorStatsVisible: true,
isStudentStatsVisible: true,
isStageInterviewFeedbackVisible: false,
Expand All @@ -197,33 +123,12 @@ describe('getPermissions', () => {
role: 'coursemanager',
permissions: {
isProfileVisible: { all: true },
isAboutVisible: { all: false, mentor: true, student: true },
isEducationVisible: { all: false, mentor: false, student: false },
isEnglishVisible: { all: false, student: false },
isEmailVisible: { all: false, student: false },
isTelegramVisible: { all: false, student: true },
isSkypeVisible: { all: false, student: true },
isPhoneVisible: { all: false, student: false },
isContactsNotesVisible: { all: true, student: true },
isLinkedInVisible: { all: false, mentor: false, student: false },
isPublicFeedbackVisible: { all: false, mentor: true, student: true },
isMentorStatsVisible: { all: false, mentor: true, student: true },
isStudentStatsVisible: { all: false, student: true },
},
}),
).toEqual({
isProfileVisible: true,
isAboutVisible: true,
isEducationVisible: true,
isEnglishVisible: true,
isEmailVisible: true,
isTelegramVisible: true,
isWhatsAppVisible: true,
isSkypeVisible: true,
isPhoneVisible: true,
isContactsNotesVisible: true,
isLinkedInVisible: true,
isPublicFeedbackVisible: true,
isMentorStatsVisible: true,
isStudentStatsVisible: true,
isStageInterviewFeedbackVisible: true,
Expand All @@ -242,33 +147,12 @@ describe('getPermissions', () => {
role: 'all',
permissions: {
isProfileVisible: { all: false },
isAboutVisible: { all: false, mentor: false, student: false },
isEducationVisible: { all: false, mentor: false, student: false },
isEnglishVisible: { all: false, student: false },
isEmailVisible: { all: false, student: false },
isTelegramVisible: { all: false, student: false },
isSkypeVisible: { all: false, student: false },
isPhoneVisible: { all: false, student: false },
isContactsNotesVisible: { all: false, student: false },
isLinkedInVisible: { all: false, mentor: false, student: false },
isPublicFeedbackVisible: { all: false, mentor: false, student: false },
isMentorStatsVisible: { all: false, mentor: false, student: false },
isStudentStatsVisible: { all: false, student: false },
},
}),
).toEqual({
isProfileVisible: true,
isAboutVisible: true,
isEducationVisible: true,
isEnglishVisible: true,
isEmailVisible: true,
isTelegramVisible: true,
isWhatsAppVisible: true,
isSkypeVisible: true,
isPhoneVisible: true,
isContactsNotesVisible: true,
isLinkedInVisible: true,
isPublicFeedbackVisible: true,
isMentorStatsVisible: true,
isStudentStatsVisible: true,
isStageInterviewFeedbackVisible: false,
Expand Down Expand Up @@ -420,44 +304,3 @@ describe('defineRole', () => {
});
});
});

describe('getProfilePermissionsSettings', () => {
it('Should be an instance of Function', () => {
expect(defineRole).toBeInstanceOf(Function);
});

it('Should not mutate param "permissions"', () => {
const permissions = {
isProfileVisible: { all: true },
};
const permissionsSettings = getProfilePermissionsSettings(permissions);

expect(permissions).toEqual({ isProfileVisible: { all: true } });
expect(permissionsSettings).not.toEqual({ isProfileVisible: { all: true } });
});

it('Should return permissions settings with defaults if all have not been passed', () => {
const permissions = {
isProfileVisible: { all: false },
isAboutVisible: { all: true, mentor: true, student: true },
isEducationVisible: { all: true, mentor: true, student: true },
};
const permissionsSettings = getProfilePermissionsSettings(permissions);

expect(permissionsSettings).toEqual({
isProfileVisible: { all: false },
isAboutVisible: { all: true, mentor: true, student: true },
isEducationVisible: { all: true, mentor: true, student: true },
isEnglishVisible: { all: false, student: false },
isEmailVisible: { all: false, student: true },
isTelegramVisible: { all: false, student: true },
isSkypeVisible: { all: false, student: true },
isPhoneVisible: { all: false, student: true },
isContactsNotesVisible: { all: false, student: true },
isLinkedInVisible: { all: false, mentor: false, student: false },
isPublicFeedbackVisible: { all: false, mentor: false, student: false },
isMentorStatsVisible: { all: false, mentor: false, student: false },
isStudentStatsVisible: { all: false, student: false },
});
});
});
37 changes: 16 additions & 21 deletions server/src/routes/profile/info.ts
Original file line number Diff line number Diff line change
@@ -1,17 +1,14 @@
import { NOT_FOUND, OK, FORBIDDEN } from 'http-status-codes';
import { NOT_FOUND, OK, FORBIDDEN, BAD_REQUEST } from 'http-status-codes';
import Router from '@koa/router';
import { ILogger } from '../../logger';
import { setResponse } from '../utils';
import { IUserSession } from '../../models';
import { ConfigurableProfilePermissions } from '../../../../common/models/profile';
import { getMentorStats } from './mentor-stats';
import { getPublicFeedback } from './public-feedback';
import { getStageInterviewFeedback } from './stage-interview-feedback';
import { getStudentStats } from './student-stats';
import { getUserInfo } from './user-info';
import {
getProfilePermissionsSettings,
getConfigurableProfilePermissions,
getRelationsRoles,
getStudentCourses,
getPermissions,
Expand All @@ -22,8 +19,13 @@ import {
} from './permissions';

export const getProfileInfo = (_: ILogger) => async (ctx: Router.RouterContext) => {
const session = ctx.state!.user as IUserSession;
const { githubId: userGithubId, isAdmin } = ctx.state!.user as IUserSession;
if (!ctx.state.user) {
setResponse(ctx, BAD_REQUEST);
return;
}

const session = ctx.state.user as IUserSession;
const { githubId: userGithubId, isAdmin } = session;
const { githubId: requestedGithubId = userGithubId } = ctx.query as { githubId: string | undefined };

if (!requestedGithubId) {
Expand All @@ -32,46 +34,39 @@ export const getProfileInfo = (_: ILogger) => async (ctx: Router.RouterContext)

const isProfileOwner = requestedGithubId === userGithubId;

const profilePermissions = await getConfigurableProfilePermissions(requestedGithubId);

let role: RelationRole;
let permissions: Permissions;
let permissionsSettings: ConfigurableProfilePermissions | undefined;

if (isProfileOwner) {
role = 'all';
permissions = getPermissions({ isProfileOwner, isAdmin });
permissionsSettings = getProfilePermissionsSettings(profilePermissions);
} else {
const relationsRoles = await getRelationsRoles(userGithubId, requestedGithubId);
const [studentCourses, registryCourses] = !relationsRoles
? await Promise.all([getStudentCourses(requestedGithubId), getMentorCourses(requestedGithubId)])
: [null, null];
role = defineRole({ relationsRoles, studentCourses, registryCourses, session, userGithubId });
permissions = getPermissions({ isAdmin, isProfileOwner, role, permissions: profilePermissions });
permissions = getPermissions({ isAdmin, isProfileOwner, role });
}

const {
isProfileVisible,
isPublicFeedbackVisible,
isMentorStatsVisible,
isStudentStatsVisible,
isStageInterviewFeedbackVisible,
} = permissions;
const { isProfileVisible, isMentorStatsVisible, isStudentStatsVisible, isStageInterviewFeedbackVisible } =
permissions;

if (!isProfileVisible && !isProfileOwner) {
return setResponse(ctx, FORBIDDEN);
}

const { generalInfo, contacts, discord } = await getUserInfo(requestedGithubId, permissions);
const publicFeedback = isPublicFeedbackVisible ? await getPublicFeedback(requestedGithubId) : undefined;
const isEpamEmailVisible = isAdmin || ['all', 'coursemanager'].includes(role);

const { generalInfo, contacts, discord } = await getUserInfo(requestedGithubId, isEpamEmailVisible);
const publicFeedback = await getPublicFeedback(requestedGithubId);
const mentorStats = isMentorStatsVisible ? await getMentorStats(requestedGithubId) : undefined;
const studentStats = isStudentStatsVisible ? await getStudentStats(requestedGithubId, permissions) : undefined;
const stageInterviewFeedback = isStageInterviewFeedbackVisible
? await getStageInterviewFeedback(requestedGithubId)
: undefined;

const profileInfo = {
permissionsSettings,
generalInfo,
contacts,
discord,
Expand Down
Loading

0 comments on commit ea2b701

Please sign in to comment.