<commit_before>/*
 Copyright 2017 <NAME>

 Licensed under the Apache License, Version 2.0 (the "License");
 you may not use this file except in compliance with the License.
 You may obtain a copy of the License at

 http://www.apache.org/licenses/LICENSE-2.0

 Unless required by applicable law or agreed to in writing, software
 distributed under the License is distributed on an "AS IS" BASIS,
 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 See the License for the specific language governing permissions and
 limitations under the License.
 */

'use strict';

const dbUser = require('../db/connectors/user');
const dbWallet = require('../db/connectors/wallet');
const { dbConfig, appConfig } = require('../config/defaults/config');
const errorCreator = require('../error/errorCreator');
const textTools = require('../utils/textTools');
const authenticator = require('../helpers/authenticator');
const roomManager = require('./rooms');
const dbRoom = require('../db/connectors/room');
const socketUtils = require('../utils/socketIo');
const dbForum = require('../db/connectors/forum');
const managerHelper = require('../helpers/manager');
const positionManager = require('./positions');
const imager = require('../helpers/imager');

/**
 * Create a user.
 * @param {Object} params Parameters.
 * @param {Object} params.user User to create.
 * @param {Function} params.callback Callback.
 * @param {Object} params.io Socket.io.
 */
function createUser({
  token,
  user,
  callback,
  io,
  options,
  socket,
  image,
}) {
  let command;

  if (appConfig.mode === appConfig.Modes.TEST) {
    command = dbConfig.apiCommands.AnonymousCreation;
  } else if (appConfig.disallowUserRegister) {
    command = dbConfig.apiCommands.CreateDisallowedUser;
  } else {
    command = dbConfig.apiCommands.CreateUser;
  }

  authenticator.isUserAllowed({
    token,
    commandName: command.name,
    callback: ({ error, data }) => {
      if (error) {
        callback({ error });

        return;
      }

      const { user: authUser } = data;

      if (!textTools.isAllowedFull(user.username)) {
        callback({
          error: new errorCreator.InvalidCharacters({ name: `User name: ${user.username}` }),
        });

        return;
      }

      if (user.username.length < appConfig.usernameMinLength || user.username.length > appConfig.usernameMaxLength) {
        callback({
          error: new errorCreator.InvalidLength({
            name: `User name length: ${appConfig.usernameMinLength}-${appConfig.usernameMaxLength}`,
            extraData: { param: 'username' },
          }),
        });

        return;
      }

      if (user.fullName && (user.fullName.length < appConfig.fullNameMinLength || user.fullName.length > appConfig.fullNameMaxLength)) {
        callback({<commit_msg>Check if full name contains valid characters<commit_after>/*
 Copyright 2017 <NAME>

 Licensed under the Apache License, Version 2.0 (the "License");
 you may not use this file except in compliance with the License.
 You may obtain a copy of the License at

 http://www.apache.org/licenses/LICENSE-2.0

 Unless required by applicable law or agreed to in writing, software
 distributed under the License is distributed on an "AS IS" BASIS,
 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 See the License for the specific language governing permissions and
 limitations under the License.
 */

'use strict';

const dbUser = require('../db/connectors/user');
const dbWallet = require('../db/connectors/wallet');
const { dbConfig, appConfig } = require('../config/defaults/config');
const errorCreator = require('../error/errorCreator');
const textTools = require('../utils/textTools');
const authenticator = require('../helpers/authenticator');
const roomManager = require('./rooms');
const dbRoom = require('../db/connectors/room');
const socketUtils = require('../utils/socketIo');
const dbForum = require('../db/connectors/forum');
const managerHelper = require('../helpers/manager');
const positionManager = require('./positions');
const imager = require('../helpers/imager');

/**
 * Create a user.
 * @param {Object} params Parameters.
 * @param {Object} params.user User to create.
 * @param {Function} params.callback Callback.
 * @param {Object} params.io Socket.io.
 */
function createUser({
  token,
  user,
  callback,
  io,
  options,
  socket,
  image,
}) {
  let command;

  if (appConfig.mode === appConfig.Modes.TEST) {
    command = dbConfig.apiCommands.AnonymousCreation;
  } else if (appConfig.disallowUserRegister) {
    command = dbConfig.apiCommands.CreateDisallowedUser;
  } else {
    command = dbConfig.apiCommands.CreateUser;
  }

  authenticator.isUserAllowed({
    token,
    commandName: command.name,
    callback: ({ error, data }) => {
      if (error) {
        callback({ error });

        return;
      }

      const { user: authUser } = data;

      if (!textTools.isAllowedFull(user.username)) {
        callback({
          error: new errorCreator.InvalidCharacters({ name: `User name: ${user.username}.` }),
        });

        return;
      }

      if (user.fullName && !textTools.isAllowedFull(user.fullName)) {
        callback({
          error: new errorCreator.InvalidCharacters({
            name: `Full name: ${user.fullName}.`,
            extraData: { param: 'fullName' },
          }),
        });

        return;
      }

      if (user.username.length < appConfig.usernameMinLength || user.username.length > appConfig.usernameMaxLength) {
        callback({
          error: new errorCreator.InvalidLength({
            name: `User name length: ${appConfig.usernameMinLength}-${appConfig.usernameMaxLength}`,
            extraData: { param: 'username' },
          }),
        });

        return;
      }

      if (user.fullName && (user.fullName.length < appConfig.fullNameMinLength || user.fullName.length > appConfig.fullNameMaxLength)) {
        callback({
{'fraction_non_alphanumeric': 0.13193629505448448, 'fraction_numerical': 0.002682313495389774, 'mean_word_length': 3.2073342736248236, 'pattern_counts': {'":': 0, '<': 9, '<?xml version=': 0, '>': 11, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, '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': '5585558', 'n_tokens_mistral': 1663, 'n_tokens_neox': 1642, 'n_words': 462}