Skip to content

Commit

Permalink
Merge branch 'refs/heads/master' into feat-session-manager
Browse files Browse the repository at this point in the history
# Conflicts:
#	graphql/authentication.ts
#	graphql/user/fields.ts
  • Loading branch information
realmayus committed Sep 3, 2024
2 parents 57d5a1c + 1d188f4 commit 681ed34
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 24 deletions.
14 changes: 5 additions & 9 deletions graphql/authentication.ts
Original file line number Diff line number Diff line change
Expand Up @@ -113,20 +113,16 @@ function ensureSession(context: GraphQLContext) {
}
}

export async function loginAsUser(user: User, context: GraphQLContext, secretID: number, noSession = false) {
if (!noSession) {
ensureSession(context);
}
export async function loginAsUser(user: User, context: GraphQLContext, secretID: number) {
ensureSession(context);

const roles = await evaluateUserRoles(user);

context.user = { ...user, secretID, roles };

if (!noSession) {
await userSessions.set(context.sessionToken, context.user);
logger.info(`[${context.sessionToken}] User(${user.userID}) successfully logged in`);
await updateLastLogin(user);
}
await userSessions.set(context.sessionToken, context.user);
logger.info(`[${context.sessionToken}] User(${user.userID}) successfully logged in`);
await updateLastLogin(user);
}

@Resolver((of) => UserType)
Expand Down
17 changes: 2 additions & 15 deletions graphql/user/fields.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@ import {
notification_channel_enum as NotificationChannelEnum,
} from '../generated';
import { Root, Authorized, FieldResolver, Query, Resolver, Arg, Ctx, ObjectType, Field, Int } from 'type-graphql';
import { UNAUTHENTICATED_USER, loginAsUser } from '../authentication';
import { GraphQLContext } from '../context';
import { Role } from '../authorizations';
import { prisma } from '../../common/prisma';
Expand All @@ -21,7 +20,6 @@ import { queryUser, User, userForPupil, userForStudent } from '../../common/user
import { UserType } from '../types/user';
import { JSONResolver } from 'graphql-scalars';
import { ACCUMULATED_LIMIT, LimitedQuery, LimitEstimated } from '../complexity';
import { DEFAULT_PREFERENCES } from '../../common/notification/defaultPreferences';
import { findUsers } from '../../common/user/search';
import { getAppointmentsForUser, getEdgeAppointmentId, hasAppointmentsForUser } from '../../common/appointment/get';
import { getMyContacts, UserContactType } from '../../common/chat/contacts';
Expand All @@ -34,8 +32,8 @@ import { Deprecated, Doc } from '../util';
import { createChatSignature } from '../../common/chat/create';
import assert from 'assert';
import { getPushSubscriptions, publicKey } from '../../common/notification/channels/push';
import _ from 'lodash';
import { getUserNotificationPreferences } from '../../common/notification';
import { evaluateUserRoles } from '../../common/user/evaluate_roles';

@ObjectType()
export class UserContact implements UserContactType {
Expand Down Expand Up @@ -117,18 +115,7 @@ export class UserFieldsResolver {
@FieldResolver((returns) => [String])
@Authorized(Role.ADMIN)
async roles(@Root() user: User) {
const fakeContext: GraphQLContext = {
user: UNAUTHENTICATED_USER,
ip: '?',
prisma,
sessionToken: 'fake',
setCookie: () => {
/* ignore */
},
sessionID: 'FAKE',
};
await loginAsUser(user, fakeContext, undefined, true);
return fakeContext.user.roles;
return await evaluateUserRoles(user);
}

// -------- Notifications ---------------------
Expand Down

0 comments on commit 681ed34

Please sign in to comment.