<commit_before>const { Forbidden, BadRequest, NotFound } = require('@feathersjs/errors');
const bcrypt = require('bcryptjs');
const { ObjectId } = require('mongoose').Types;
const { checkPasswordStrength } = require('../../../utils/passwordHelpers');
const { equal: equalIds } = require('../../../helper/compare').ObjectId;
const constants = require('../../../utils/constants');

const globalHooks = require('../../../hooks');

const { LdapStrategy, MoodleStrategy, IservStrategy } = require('../../authentication/strategies');

const strategies = {
	moodle: MoodleStrategy,
	ldap: LdapStrategy,
	iserv: IservStrategy,
};

const sanitizeUsername = (hook) => {
	if (hook.data && hook.data.username && !hook.data.systemId) {
		hook.data.username = hook.data.username.trim().toLowerCase();
	}
	return hook;
};

// This is only for SSO
const validateCredentials = async (hook) => {
	const { username, password, systemId } = hook.data;

	if (!username) throw new BadRequest('no username specified');
	if (!password) throw new BadRequest('no password specified');

	if (!systemId) return hook;

	const { app } = hook;
	const systemService = app.service('/systems');
	const system = await systemService.get(systemId);

	const Strategy = strategies[system.type];
	const systemStrategy = new Strategy();
	const client = await systemStrategy.credentialCheck(username, password, system);
	if (client) {
		return hook;
	}
	return Promise.reject();
};

const trimPassword = (hook) => {
	if (hook.data.password) {
		hook.data.password = hook.data.password.trim();
	}
	if (hook.data.password_verification) {
		hook.data.password_verification = hook.data.password_verification.trim();
	}

	return hook;
};

const validatePassword = (hook) => {
	const passwordVerification = hook.data.password_verification;
	const { password } = hook.data;

	// Check against Pattern which is also used in Frontend
	const patternResult = checkPasswordStrength(password);

	// only check result if also a password was really given
	if (!patternResult && password) {
		throw new BadRequest('Dein Passwort stimmt mit dem Pattern nicht überein.');
	}

	// in case sso created account skip
	if (!hook.params.account.userId) {
		return hook;
	}

	return Promise.all([
		globalHooks.hasPermissionNoHook(hook, hook.params.account.userId, 'STUDENT_CREATE'),
		globalHooks.hasRoleNoHook(hook, hook.id, 'student', true),
		globalHooks.hasPermissionNoHook(hook, hook.params.account.userId, 'ADMIN_VIEW'),
		globalHooks.hasRoleNoHook(hook, hook.id, 'teacher', true),
		globalHooks.hasRole(hook, hook.params.account.userId, 'superhero'),
		hook.app.service('users').get(hook.params.account.userId),
	]).then(([hasStudentCreate, isStudent, hasAdminView, isTeacher, isSuperHero, user]) => {<commit_msg>change permission<commit_after>const { Forbidden, BadRequest, NotFound } = require('@feathersjs/errors');
const bcrypt = require('bcryptjs');
const { ObjectId } = require('mongoose').Types;
const { checkPasswordStrength } = require('../../../utils/passwordHelpers');
const { equal: equalIds } = require('../../../helper/compare').ObjectId;
const constants = require('../../../utils/constants');

const globalHooks = require('../../../hooks');

const { LdapStrategy, MoodleStrategy, IservStrategy } = require('../../authentication/strategies');

const strategies = {
	moodle: MoodleStrategy,
	ldap: LdapStrategy,
	iserv: IservStrategy,
};

const sanitizeUsername = (hook) => {
	if (hook.data && hook.data.username && !hook.data.systemId) {
		hook.data.username = hook.data.username.trim().toLowerCase();
	}
	return hook;
};

// This is only for SSO
const validateCredentials = async (hook) => {
	const { username, password, systemId } = hook.data;

	if (!username) throw new BadRequest('no username specified');
	if (!password) throw new BadRequest('no password specified');

	if (!systemId) return hook;

	const { app } = hook;
	const systemService = app.service('/systems');
	const system = await systemService.get(systemId);

	const Strategy = strategies[system.type];
	const systemStrategy = new Strategy();
	const client = await systemStrategy.credentialCheck(username, password, system);
	if (client) {
		return hook;
	}
	return Promise.reject();
};

const trimPassword = (hook) => {
	if (hook.data.password) {
		hook.data.password = hook.data.password.trim();
	}
	if (hook.data.password_verification) {
		hook.data.password_verification = hook.data.password_verification.trim();
	}

	return hook;
};

const validatePassword = (hook) => {
	const passwordVerification = hook.data.password_verification;
	const { password } = hook.data;

	// Check against Pattern which is also used in Frontend
	const patternResult = checkPasswordStrength(password);

	// only check result if also a password was really given
	if (!patternResult && password) {
		throw new BadRequest('Dein Passwort stimmt mit dem Pattern nicht überein.');
	}

	// in case sso created account skip
	if (!hook.params.account.userId) {
		return hook;
	}

	return Promise.all([
		globalHooks.hasPermissionNoHook(hook, hook.params.account.userId, 'STUDENT_EDIT'),
		globalHooks.hasRoleNoHook(hook, hook.id, 'student', true),
		globalHooks.hasPermissionNoHook(hook, hook.params.account.userId, 'ADMIN_VIEW'),
		globalHooks.hasRoleNoHook(hook, hook.id, 'teacher', true),
		globalHooks.hasRole(hook, hook.params.account.userId, 'superhero'),
		hook.app.service('users').get(hook.params.account.userId),
	]).then(([hasStudentCreate, isStudent, hasAdminView, isTeacher, isSuperHero, user]) => {
{'fraction_non_alphanumeric': 0.14923747276688454, 'fraction_numerical': 0.0, 'mean_word_length': 4.811181434599156, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 13, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '912167', 'n_tokens_mistral': 1712, 'n_tokens_neox': 1687, 'n_words': 405}