<commit_before>import toastr from 'toastr';

Template.userEdit.helpers({
	canEditOrAdd() {
		return (Template.instance().user && RocketChat.authz.hasAtLeastOnePermission('edit-other-user-info')) || (!Template.instance().user && RocketChat.authz.hasAtLeastOnePermission('create-user'));
	},

	user() {
		return Template.instance().user;
	},

	requirePasswordChange() {
		return !Template.instance().user || Template.instance().user.requirePasswordChange;
	},

	role() {
		return RocketChat.models.Roles.find({}, { sort: { description: 1, _id: 1 } });
	},

	userRoles() {
		return Template.instance().user.roles;
	},

	selectUserRole() {
		console.log(Template.parentData(1));
		if (this._id === Template.parentData(1)) {
			return 'selected';
		}
	},

	name() {
		return this.description || this._id;
	}
});

Template.userEdit.events({
	'click .cancel'(e, t) {
		e.stopPropagation();
		e.preventDefault();
		return t.cancel(t.find('form'));
	},

	'click .remove-role'(e) {
		console.log(this);
		e.stopPropagation();
		e.preventDefault();
		$(`[data=${ this }]`).remove();
	},

	'click #randomPassword'(e) {
		e.stopPropagation();
		e.preventDefault();
		return $('#password').val(Random.id());
	},

	'submit form'(e, t) {
		e.stopPropagation();
		e.preventDefault();
		return t.save(e.currentTarget);
	}
});

Template.userEdit.onCreated(function() {
	let userData;
	this.user = this.data != null ? this.data.user : undefined;

	const { tabBar } = Template.currentData();

	this.cancel = (form, username) => {
		form.reset();
		this.$('input[type=checkbox]').prop('checked', true);
		if (this.user) {
			return this.data.back(username);
		} else {
			return tabBar.close();
		}
	};

	this.getUserData = () => {
		userData = { _id: (this.user != null ? this.user._id : undefined) };
		userData.name = s.trim(this.$('#name').val());
		userData.username = s.trim(this.$('#username').val());
		userData.email = s.trim(this.$('#email').val());
		userData.verified = this.$('#verified:checked').length > 0;
		userData.password = s.<PASSWORD>(this.$('#password').val());
		userData.requirePasswordChange = this.$('#changePassword:checked').length > 0;
		userData.joinDefaultChannels = this.$('#joinDefaultChannels:checked').length > 0;
		userData.sendWelcomeEmail = this.$('#sendWelcomeEmail:checked').length > 0;
		// userData.roles = this.$('.role-select');
		const roleSelect = this.$('.role-select');
		if (roleSelect.length > 0) {
			const roles = roleSelect.map(role => {
				return role.value;
			});

			console.log(roles);
		}



		// if (this.$('#role').val()) {
		// 	userData.roles = [this.$('#role').val()];
		// }
		return userData;
	};

	this.validate = () => {
		userData = this.getUserData();
<commit_msg>support for multiple roles<commit_after>import toastr from 'toastr';

Template.userEdit.helpers({
	canEditOrAdd() {
		return (Template.instance().user && RocketChat.authz.hasAtLeastOnePermission('edit-other-user-info')) || (!Template.instance().user && RocketChat.authz.hasAtLeastOnePermission('create-user'));
	},

	user() {
		return Template.instance().user;
	},

	requirePasswordChange() {
		return !Template.instance().user || Template.instance().user.requirePasswordChange;
	},

	role() {
		return RocketChat.models.Roles.find({}, { sort: { description: 1, _id: 1 } });
	},

	userRoles() {
		return Template.instance().user.roles;
	},

	selectUserRole() {
		console.log(Template.parentData(1));
		if (this._id === Template.parentData(1)) {
			return 'selected';
		}
	},

	name() {
		return this.description || this._id;
	}
});

Template.userEdit.events({
	'click .cancel'(e, t) {
		e.stopPropagation();
		e.preventDefault();
		return t.cancel(t.find('form'));
	},

	'click .remove-role'(e) {
		console.log(this);
		e.stopPropagation();
		e.preventDefault();
		$(`[data=${ this }]`).remove();
	},

	'click #randomPassword'(e) {
		e.stopPropagation();
		e.preventDefault();
		return $('#password').val(Random.id());
	},

	'submit form'(e, t) {
		e.stopPropagation();
		e.preventDefault();
		return t.save(e.currentTarget);
	}
});

Template.userEdit.onCreated(function() {
	let userData;
	this.user = this.data != null ? this.data.user : undefined;

	const { tabBar } = Template.currentData();

	this.cancel = (form, username) => {
		form.reset();
		this.$('input[type=checkbox]').prop('checked', true);
		if (this.user) {
			return this.data.back(username);
		} else {
			return tabBar.close();
		}
	};

	this.getUserData = () => {
		userData = { _id: (this.user != null ? this.user._id : undefined) };
		userData.name = s.trim(this.$('#name').val());
		userData.username = s.trim(this.$('#username').val());
		userData.email = s.trim(this.$('#email').val());
		userData.verified = this.$('#verified:checked').length > 0;
		userData.password = <PASSWORD>(this.$('#password').val());
		userData.requirePasswordChange = this.$('#changePassword:checked').length > 0;
		userData.joinDefaultChannels = this.$('#joinDefaultChannels:checked').length > 0;
		userData.sendWelcomeEmail = this.$('#sendWelcomeEmail:checked').length > 0;
		// userData.roles = this.$('.role-select').toArray();
		const roleSelect = this.$('.role-select').toArray();
		if (roleSelect.length > 0) {
			userData.roles = roleSelect.map(role => {
				return role.value;
			});

			// console.log(roles);
		}



		// if (this.$('#role').val()) {
		// 	userData.roles = [this.$('#role').val()];
		// }
		return userData;
	};

	this.validate = () => {
		userData = this.getUserData();

{'fraction_non_alphanumeric': 0.21043000914913082, 'fraction_numerical': 0.003293687099725526, 'mean_word_length': 4.127579737335835, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 23, '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': '5447176', 'n_tokens_mistral': 2025, 'n_tokens_neox': 1876, 'n_words': 288}