<commit_before>Meteor.methods
	setUsername: (username) ->
		if not Meteor.userId()
			throw new Meteor.Error('invalid-user', "[methods] setUsername -> Invalid user")

		unless RocketChat.settings.get("Accounts_AllowUsernameChange")
			throw new Meteor.Error(403, "[methods] resetAvatar -> Invalid access")

		console.log '[methods] setUsername -> '.green, 'userId:', Meteor.userId(), 'arguments:', arguments

		user = Meteor.user()

		if user.username is username
			return username

		try
			nameValidation = new RegExp '^' + RocketChat.settings.get('UTF8_Names_Validation') + '$'
		catch
			nameValidation = new RegExp '^[0-9a-zA-Z-_.]+$'

		if not nameValidation.test username
			throw new Meteor.Error 'username-invalid', "#{username} is not a valid username, use only letters, numbers, dots and dashes"

		if not RocketChat.checkUsernameAvailability username
			throw new Meteor.Error 'username-unavailable', "#{username} is already in use :("

		unless RocketChat.setUsername user, username
			throw new Meteor.Error 'could-not-change-username', "Could not change username"

		return username

# Limit setting username once per minute
DDPRateLimiter.addRule
	type: 'method'
	name: 'setUsername'
	userId: (userId) ->
		# Administrators have permission to change others usernames, so don't limit those
		return not RocketChat.authz.hasPermission( userId, 'edit-other-user-info')<commit_msg>Fixed error: when allow change username was set to false, registration failed.<commit_after>Meteor.methods
	setUsername: (username) ->
		if not Meteor.userId()
			throw new Meteor.Error('invalid-user', "[methods] setUsername -> Invalid user")

		user = Meteor.user()

		if user.username? and not RocketChat.settings.get("Accounts_AllowUsernameChange")
			throw new Meteor.Error(403, "[methods] setUsername -> Username change not allowed")

		console.log '[methods] setUsername -> '.green, 'userId:', Meteor.userId(), 'arguments:', arguments

		if user.username is username
			return username

		try
			nameValidation = new RegExp '^' + RocketChat.settings.get('UTF8_Names_Validation') + '$'
		catch
			nameValidation = new RegExp '^[0-9a-zA-Z-_.]+$'

		if not nameValidation.test username
			throw new Meteor.Error 'username-invalid', "#{username} is not a valid username, use only letters, numbers, dots and dashes"

		if not RocketChat.checkUsernameAvailability username
			throw new Meteor.Error 'username-unavailable', "#{username} is already in use :("

		unless RocketChat.setUsername user, username
			throw new Meteor.Error 'could-not-change-username', "Could not change username"

		return username

# Limit setting username once per minute
DDPRateLimiter.addRule
	type: 'method'
	name: 'setUsername'
	userId: (userId) ->
		# Administrators have permission to change others usernames, so don't limit those
		return not RocketChat.authz.hasPermission( userId, 'edit-other-user-info')
{'fraction_non_alphanumeric': 0.1119584055459272, 'fraction_numerical': 0.004159445407279029, 'mean_word_length': 4.6923076923076925, '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': '6269085', 'n_tokens_mistral': 937, 'n_tokens_neox': 837, 'n_words': 265}