Skip to content
Snippets Groups Projects
Commit 4e9a33fa authored by Olivér FACKLAM's avatar Olivér FACKLAM
Browse files

userModel.ts

parent 4cddd011
No related branches found
No related tags found
No related merge requests found
......@@ -181,6 +181,7 @@ app.post('/login', (req, res, next) => {
// See: https://www.apollographql.com/docs/apollo-server/features/authentication.html
import { Context } from './graphql/typeDefs/queries';
import { AuthorisationModel } from './graphql/connectors/authorisationModel';
import { UserModel } from './graphql/connectors/userModel';
const context = async ({ req }): Promise<Context> => {
// set a special uid for non-authenticated requests
// /!\ FOR DEVELOPMENT ONLY: use the one in the ldap config .json file
......@@ -202,12 +203,15 @@ const context = async ({ req }): Promise<Context> => {
console.log(err);
}
}
let authModule = await AuthorisationModel.create(uid);
return {
request: req,
user: { uid: uid },
models: {
auth: await AuthorisationModel.create(uid)
auth: authModule,
user: new UserModel(authModule)
}
};
};
......
......@@ -61,6 +61,7 @@ export class AuthorisationModel {
/**
* @memberof GraphQL.AuthorisationModel#
* @function fetchData
* @summary Fonction qui recupere toutes les données pour populer les champs a partir de l'uid.
* @async
*/
......@@ -76,6 +77,17 @@ export class AuthorisationModel {
/**
* @memberof GraphQL.AuthorisationModel#
* @function getUid
* @summary Fonction qui renvoit l'identifiant de l'utilisateur
* @return {string} Renvoie l'uid de l'utilisateur du 'context'
*/
getUid(): string {
return this.uid;
}
/**
* @memberof GraphQL.AuthorisationModel#
* @function isConnectedOrOnplatal
* @summary Fonction qui renvoit si l'utilisateur est connecté ou on-platal
* @return {boolean} Renvoie true si l'utilisateur est connecté ou on-platal
*/
......@@ -85,6 +97,7 @@ export class AuthorisationModel {
/**
* @memberof GraphQL.AuthorisationModel#
* @function isAuthenticated
* @summary Fonction qui renvoit si l'utilisateur est authentifié
* @return {boolean} Renvoie true si l'utilisateur est authentifié
*/
......@@ -94,6 +107,7 @@ export class AuthorisationModel {
/**
* @memberof GraphQL.AuthorisationModel#
* @function isViewer
* @summary Fonction qui renvoit si l'utilisateur est viewer du groupe.
* @arg {string} gid - Identifiant du groupe.
* @return {boolean} Renvoie true si l'utilisateur est viewer du groupe.
......@@ -109,6 +123,7 @@ export class AuthorisationModel {
/**
* @memberof GraphQL.AuthorisationModel#
* @function isMember
* @summary Fonction qui renvoit si l'utilisateur est membre du groupe.
* @arg {string} gid - Identifiant du groupe.
* @return {boolean} Renvoie true si l'utilisateur est membre du groupe.
......@@ -124,6 +139,7 @@ export class AuthorisationModel {
/**
* @memberof GraphQL.AuthorisationModel#
* @function isMemberOr
* @summary Fonction qui renvoit si l'utilisateur est membre d'au moins un de ces groupes.
* @arg {GroupSet} groups - Ensemble de groupes.
* @return {boolean} Renvoie true si l'utilisateur est membre d'un des groupes
......@@ -144,6 +160,7 @@ export class AuthorisationModel {
/**
* @memberof GraphQL.AuthorisationModel#
* @function isSpeaker
* @summary Fonction qui renvoit si l'utilisateur est speaker du groupe.
* @arg {string} gid - Identifiant du groupe.
* @return {boolean} Renvoie true si l'utilisateur est speaker du groupe.
......@@ -159,6 +176,7 @@ export class AuthorisationModel {
/**
* @memberof GraphQL.AuthorisationModel#
* @function isAdmin
* @summary Fonction qui renvoit si l'utilisateur est admin du groupe.
* @arg {string} gid - Identifiant du groupe.
* @return {boolean} Renvoie true si l'utilisateur est admin du groupe.
......@@ -174,6 +192,7 @@ export class AuthorisationModel {
/**
* @memberof GraphQL.AuthorisationModel#
* @function isSupervisor
* @summary Fonction qui renvoit si l'utilisateur est supervisor du groupe.
* @arg {string} gid - Identifiant du groupe.
* @return {boolean} Renvoie true si l'utilisateur est supervisor du groupe.
......
/**
* @file Fonctions qui implémentent les requetes relatives aux utilisateurs
* @author ofacklam
* @memberof GraphQL
*/
import { AuthorisationModel } from "./authorisationModel";
import { User } from "../resolvers/users";
import { User as UT, userData } from "../../ldap/export/user"
import { searchTOLArgs, editProfileArgs } from "../typeDefs/queries";
export class UserModel {
/**
* @memberof GraphQL
* @class UserModel
* @summary Requetes relatives aux utilisateurs.
* @classdesc Cette classe contient les méthodes implémentant les requetes relatives aux utilisateurs.
* @arg {AuthorisationModel} auth - Le module d'auth
*/
constructor(auth: AuthorisationModel) {
this.auth = auth;
}
protected auth: AuthorisationModel;
/**
* @memberof GraphQL.UserModel#
* @function getUser
* @summary Fonction qui renvoit un utilisateur donné.
* @arg {string} uid - Identifiant demandé.
* @return {Promise<User>} Renvoie l'utilisateur dont l'identifiant est 'uid'
* @async
* @rights connectedOrOnplatal
*/
async getUser(uid: string): Promise<User> {
if(this.auth.isConnectedOrOnplatal()) {
return User.tryCreate(uid);
}
return null;
}
/**
* @memberof GraphQL.UserModel#
* @function searchTOL
* @summary Fonction qui recherche dans le TOL
* @arg {searchTOLArgs} args - les données de recherche
* @return {Promise<User[]>} Renvoie une liste d'utilisateurs
* @async
* @rights connectedOrOnplatal
*/
async searchTOL(args: searchTOLArgs): Promise<User[]> {
if(this.auth.isConnectedOrOnplatal()) {
const searchData: userData = {
givenName: args.givenName,
lastName: args.lastName,
nickname: args.nickname,
nationality: args.nationality,
promotion: args.promotion,
groups: args.groups,
sport: args.sport,
phone: args.phone,
mail: args.mail,
address: args.addresses[0],
ips: args.ips
}
const userList = await UT.search(searchData);
return userList.map((uid) => new User(uid));
}
return null;
}
/**
* @memberof GraphQL.UserModel#
* @function editProfile
* @summary Fonction qui modifie le profil et renvoie l'utilisateur
* @arg {editProfileArgs} args - les données a modifier
* @return {Promise<User>} Renvoie l'utilisateur mis a jour
* @async
* @rights authentified
*/
async editProfile(args: editProfileArgs): Promise<User> {
if (this.auth.isAuthenticated()) {
let data = await UT.peek(this.auth.getUid());
//Modify some fields, keep the others
let editArgs: userData = {
uid: data.uid,
groups: data.groups,
groupsIsAdmin: data.groupsIsAdmin,
password: data.password,
givenName: data.givenName,
lastName: data.lastName,
nickname: args.nickname, // <- this field is modified by user
promotion: data.promotion,
photo: data.photo,
birthdate: data.birthdate,
nationality: data.nationality,
phone: args.phone, // <- this field is modified
address: data.address, // WTF why can't this be changed ????
mail: args.mail, // <- this field is modified
ips: data.ips,
directory: data.directory,
login: data.login,
readPerm: data.readPerm,
writePerm: data.writePerm,
forlifes: data.forlifes,
sport: data.sport
};
if(await UT.edit(editArgs)) {
return new User(data.uid);
}
else {
throw "Modification échouée";
}
}
return null;
}
}
\ No newline at end of file
......@@ -274,10 +274,4 @@ type Mutation {
takeAdminRights(forGroup: ID!) : Boolean
releaseAdminRights(forGroup: ID!) : Boolean
# Log user into the API, replies with a token.
login(username: String!, password: String!): String!
# Log user out of the API
logout(username: String!): Boolean
#leave(from : String!) : Boolean
}
import { AuthorisationModel } from "../connectors/authorisationModel";
import { UserModel } from "../connectors/userModel";
interface Context {
request,
user: { uid: string },
models: {
auth: AuthorisationModel,
user: UserModel
}
}
interface searchTOLArgs {
givenName: string,
......@@ -16,10 +26,8 @@ interface searchTOLArgs {
ips: string[]
}
interface Context {
request,
user: { uid: string },
models: {
auth: AuthorisationModel
}
}
\ No newline at end of file
interface editProfileArgs {
nickname: string,
mail: string,
phone: string
}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment