<commit_before><?php

/**
 * UserManager.php
 * DSP user manager
 *
 * This file is part of the DreamFactory Services Platform(tm) (DSP)
 * Copyright (c) 2009-2013 DreamFactory Software, Inc. <<EMAIL>>
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */
class UserManager implements iRestHandler
{
	/**
	 * @var UserManager
	 */
	private static $_instance = null;

	/**
	 * @var string
	 */
	protected $command;

	/**
	 * @var string
	 */
	protected static $_randKey;

	/**
	 * @var null
	 */
	private static $_userId = null;

	/**
	 * @var null
	 */
	private static $_cache = null;

	/**
	 * Create a new UserManager
	 *
	 */
	public function __construct()
	{
		//For better security. Get a random string from this link: http://tinyurl.com/randstr and put it here
		static::$_randKey = '<KEY>';
	}

	/**
	 * Object destructor
	 */
	public function __destruct()
	{
	}

	/**
	 * Gets the static instance of this class.
	 *
	 * @return UserManager
	 */
	public static function getInstance()
	{
		if ( !isset( self::$_instance ) )
		{
			self::$_instance = new UserManager();
		}

		return self::$_instance;
	}

	/**
	 * For compatibility with BaseService
	 *
	 * @return string
	 */
	public static function getType()
	{
		return 'User';
	}

	// Controller based methods

	/**
	 * @return array
	 * @throws Exception
	 */
	public function actionSwagger()
	{
		$this->detectCommonParams();
		$result = SwaggerUtilities::swaggerBaseInfo( 'user' );
		$resources = array(
			array(
				'path'        => '/user',
				'description' => "Operations on the user service",
				'operations'  => array(
					array(
						"httpMethod"     => "GET",
						"summary"        => "List resources available in the user service",
						"notes"          => "Use these resources to maintain session, update profile and change password.",
						"responseClass"  => "array",
						"nickname"       => "getResources",
						"parameters"     => array(),
						"errorResponses" => array()
					)
				)
			),
			array(
				'path'        => '/user/session',
				'description' => "Operations on a user's session",
				'operations'  => array(
					array(
						"httpMethod"     => "GET",
						"summary"        => "Retrieve the current user session information",
						"notes"          => "Calling this refreshes the current session.",
						"responseClass"  => "array",
						"nickname"       => "getSession",
						"parameters"     => array(),
						"errorResponses" => array()
					),
					array(
						"httpMethod"     => "POST",
						"summary"        => "Login and create a new user session",
						"notes"          => "Calling this creates a new session and logs in the user.",
						"responseClass"  => "array",
						"nickname"       => "login",
						"parameters"     => array(),
						"errorResponses" => array()
					),
					array(
						"httpMethod"     => "DELETE",
						"summary"        => "Logout and destroy the current user session",
						"notes"          => "Calling this deletes the current session and logs out the user.",
						"responseClass"  => "array",
						"nickname"       => "logout",
						"parameters"     => array(),
						"errorResponses" => array()
					),
				)
			),
			array(
				'path'        => '/user/profile',
				'description' => "Operations on a user's profile",
				'operations'  => array(
					array(
						"httpMethod"     => "GET",
						"summary"        => "Retrieve the current user's profile information",
						"notes"          => "This profile, along with password, is the only things that the user can directly change.",
						"responseClass"  => "array",
						"nickname"       => "getProfile",
						"parameters"     => array(),
						"errorResponses" => array()
					),
					array(
						"httpMethod"     => "POST",
						"summary"        => "Update the current user's profile information",
						"notes"          => "Update the security question and answer through this api, as well as, display name, email, etc.",
						"responseClass"  => "array",
						"nickname"       => "changeProfile",
						"parameters"     => array(),
						"errorResponses" => array()
					),
				)
			),
			array(
				'path'        => '/user/password',
				'description' => "Operations on a user's password",
				'operations'  => array(
					array(
						"httpMethod"     => "POST",
						"summary"        => "Update the current user's password",
						"notes"          => "A valid session is required to change the password through this API.",
						"responseClass"  => "array",
						"nickname"       => "changePassword",
						"parameters"     => array(),
						"errorResponses" => array()
					),
				)
			),
			array(
				'path'        => '/user/challenge',
				'description' => '',
				'operations'  => array(
					array(
						"httpMethod"     => "GET",
						"summary"        => "Retrieve the security challenge question for the given user",
						"notes"          => "Use this to retrieve the challenge question to present to the user.",
						"responseClass"  => "array",
						"nickname"       => "getChallenge",
						"parameters"     => array(),
						"errorResponses" => array()
					),
					array(
						"httpMethod"     => "POST",
						"summary"        => "Answer the security challenge question for the given user",
						"notes"          => "Use this to gain temporary access to change password.",
						"responseClass"  => "array",
						"nickname"       => "answerChallenge",
						"parameters"     => array(),
						"errorResponses" => array()
					),
				)
			),
			array(
				'path'        => '/user/register',
				'description' => '',
				'operations'  => array()
			),
			array(
				'path'        => '/user/confirm',
				'description' => '',
				'operations'  => array()
			),
			array(
				'path'        => '/user/ticket',
				'description' => '',
				'operations'  => array()
			)
		);
		$result['apis'] = $resources;

		return $result;
	}

	/**
	 * @return array
	 * @throws Exception
	 */
	public function actionGet()
	{
		$this->detectCommonParams();
		switch ( $this->command )
		{
			case '':
				$resources = array(
					array( 'name' => 'session' ),
					array( 'name' => 'profile' ),
					array( 'name' => 'password' ),
					array( 'name' => 'challenge' ),
					array( 'name' => 'register' ),
					array( 'name' => 'confirm' ),
					array( 'name' => 'ticket' )
				);
				$result = array( 'resource' => $resources );
				break;
			case 'challenge':
				$email = Utilities::getArrayValue( 'email', $_REQUEST, '' );
				$send_email = Utilities::getArrayValue( 'send_email', $_REQUEST, false );
				$result = $this->forgotPassword( $email, $send_email );
				break;
			case 'profile':
				$result = $this->getProfile();
				break;
			case 'session':
				$ticket = Utilities::getArrayValue( 'ticket', $_REQUEST, '' );
				$result = $this->userSession( $ticket );
				break;
			case 'ticket':
				$result = $this->userTicket();
				break;
			default:
				// unsupported GET request
				throw new Exception( "GET Request command '$this->command' is not currently supported by this User API.", ErrorCodes::BAD_REQUEST );
				break;
		}

		return $result;
	}

	/**
	 * @return array
	 * @throws Exception
	 */
	public function actionPost()
	{
		$this->detectCommonParams();
		$data = Utilities::getPostDataAsArray();
		switch ( $this->command )
		{
			case 'session':
				$email = Utilities::getArrayValue( 'email', $data, '' );
				$password = Utilities::getArrayValue( 'password', $data, '' );
				//$password = <PASSWORD>($password);
				$result = $this->userLogin( $email, $password );
				break;
			case 'register':
				$firstName = Utilities::getArrayValue( 'first_name', $data, '' );
				$lastName = Utilities::getArrayValue( 'last_name', $data, '' );
				$displayName = Utilities::getArrayValue( 'display_name', $data, '' );
				$email = Utilities::getArrayValue( 'email', $data, '' );
				$password = Utilities::getArrayValue( 'password', $data, '' );
				//$password = <PASSWORD>($password);
				$result = $this->userRegister( $email, $password, $firstName, $lastName, $displayName );
				break;
			case 'confirm':
				$code = Utilities::getArrayValue( 'code', $_REQUEST, '' );
				if ( empty( $code ) )
				{
					$code = Utilities::getArrayValue( 'code', $data, '' );
				}
				$email = Utilities::getArrayValue( 'email', $_REQUEST, '' );
				if ( empty( $email ) )
				{
					$email = Utilities::getArrayValue( 'email', $data, '' );
				}
				if ( empty( $email ) && !empty( $code ) )
				{
					throw new Exception( "Missing required email or code for invitation.", ErrorCodes::BAD_REQUEST );
				}
				$newPassword = Utilities::getArrayValue( 'new_password', $data, '' );
				if ( empty( $newPassword ) )
				{
					throw new Exception( "Missing required fields 'new_password'.", ErrorCodes::BAD_REQUEST );
				}
				$result = $this->userConfirm( $code );
				if ( !empty( $code ) )
				{
					$result = $this->passwordResetByCode( $code, $newPassword );
				}
				else
				{<commit_msg>confirmation bug fix<commit_after><?php

/**
 * UserManager.php
 * DSP user manager
 *
 * This file is part of the DreamFactory Services Platform(tm) (DSP)
 * Copyright (c) 2009-2013 DreamFactory Software, Inc. <<EMAIL>>
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */
class UserManager implements iRestHandler
{
	/**
	 * @var UserManager
	 */
	private static $_instance = null;

	/**
	 * @var string
	 */
	protected $command;

	/**
	 * @var string
	 */
	protected static $_randKey;

	/**
	 * @var null
	 */
	private static $_userId = null;

	/**
	 * @var null
	 */
	private static $_cache = null;

	/**
	 * Create a new UserManager
	 *
	 */
	public function __construct()
	{
		//For better security. Get a random string from this link: http://tinyurl.com/randstr and put it here
		static::$_randKey = '<KEY>';
	}

	/**
	 * Object destructor
	 */
	public function __destruct()
	{
	}

	/**
	 * Gets the static instance of this class.
	 *
	 * @return UserManager
	 */
	public static function getInstance()
	{
		if ( !isset( self::$_instance ) )
		{
			self::$_instance = new UserManager();
		}

		return self::$_instance;
	}

	/**
	 * For compatibility with BaseService
	 *
	 * @return string
	 */
	public static function getType()
	{
		return 'User';
	}

	// Controller based methods

	/**
	 * @return array
	 * @throws Exception
	 */
	public function actionSwagger()
	{
		$this->detectCommonParams();
		$result = SwaggerUtilities::swaggerBaseInfo( 'user' );
		$resources = array(
			array(
				'path'        => '/user',
				'description' => "Operations on the user service",
				'operations'  => array(
					array(
						"httpMethod"     => "GET",
						"summary"        => "List resources available in the user service",
						"notes"          => "Use these resources to maintain session, update profile and change password.",
						"responseClass"  => "array",
						"nickname"       => "getResources",
						"parameters"     => array(),
						"errorResponses" => array()
					)
				)
			),
			array(
				'path'        => '/user/session',
				'description' => "Operations on a user's session",
				'operations'  => array(
					array(
						"httpMethod"     => "GET",
						"summary"        => "Retrieve the current user session information",
						"notes"          => "Calling this refreshes the current session.",
						"responseClass"  => "array",
						"nickname"       => "getSession",
						"parameters"     => array(),
						"errorResponses" => array()
					),
					array(
						"httpMethod"     => "POST",
						"summary"        => "Login and create a new user session",
						"notes"          => "Calling this creates a new session and logs in the user.",
						"responseClass"  => "array",
						"nickname"       => "login",
						"parameters"     => array(),
						"errorResponses" => array()
					),
					array(
						"httpMethod"     => "DELETE",
						"summary"        => "Logout and destroy the current user session",
						"notes"          => "Calling this deletes the current session and logs out the user.",
						"responseClass"  => "array",
						"nickname"       => "logout",
						"parameters"     => array(),
						"errorResponses" => array()
					),
				)
			),
			array(
				'path'        => '/user/profile',
				'description' => "Operations on a user's profile",
				'operations'  => array(
					array(
						"httpMethod"     => "GET",
						"summary"        => "Retrieve the current user's profile information",
						"notes"          => "This profile, along with password, is the only things that the user can directly change.",
						"responseClass"  => "array",
						"nickname"       => "getProfile",
						"parameters"     => array(),
						"errorResponses" => array()
					),
					array(
						"httpMethod"     => "POST",
						"summary"        => "Update the current user's profile information",
						"notes"          => "Update the security question and answer through this api, as well as, display name, email, etc.",
						"responseClass"  => "array",
						"nickname"       => "changeProfile",
						"parameters"     => array(),
						"errorResponses" => array()
					),
				)
			),
			array(
				'path'        => '/user/password',
				'description' => "Operations on a user's password",
				'operations'  => array(
					array(
						"httpMethod"     => "POST",
						"summary"        => "Update the current user's password",
						"notes"          => "A valid session is required to change the password through this API.",
						"responseClass"  => "array",
						"nickname"       => "changePassword",
						"parameters"     => array(),
						"errorResponses" => array()
					),
				)
			),
			array(
				'path'        => '/user/challenge',
				'description' => '',
				'operations'  => array(
					array(
						"httpMethod"     => "GET",
						"summary"        => "Retrieve the security challenge question for the given user",
						"notes"          => "Use this to retrieve the challenge question to present to the user.",
						"responseClass"  => "array",
						"nickname"       => "getChallenge",
						"parameters"     => array(),
						"errorResponses" => array()
					),
					array(
						"httpMethod"     => "POST",
						"summary"        => "Answer the security challenge question for the given user",
						"notes"          => "Use this to gain temporary access to change password.",
						"responseClass"  => "array",
						"nickname"       => "answerChallenge",
						"parameters"     => array(),
						"errorResponses" => array()
					),
				)
			),
			array(
				'path'        => '/user/register',
				'description' => '',
				'operations'  => array()
			),
			array(
				'path'        => '/user/confirm',
				'description' => '',
				'operations'  => array()
			),
			array(
				'path'        => '/user/ticket',
				'description' => '',
				'operations'  => array()
			)
		);
		$result['apis'] = $resources;

		return $result;
	}

	/**
	 * @return array
	 * @throws Exception
	 */
	public function actionGet()
	{
		$this->detectCommonParams();
		switch ( $this->command )
		{
			case '':
				$resources = array(
					array( 'name' => 'session' ),
					array( 'name' => 'profile' ),
					array( 'name' => 'password' ),
					array( 'name' => 'challenge' ),
					array( 'name' => 'register' ),
					array( 'name' => 'confirm' ),
					array( 'name' => 'ticket' )
				);
				$result = array( 'resource' => $resources );
				break;
			case 'challenge':
				$email = Utilities::getArrayValue( 'email', $_REQUEST, '' );
				$send_email = Utilities::getArrayValue( 'send_email', $_REQUEST, false );
				$result = $this->forgotPassword( $email, $send_email );
				break;
			case 'profile':
				$result = $this->getProfile();
				break;
			case 'session':
				$ticket = Utilities::getArrayValue( 'ticket', $_REQUEST, '' );
				$result = $this->userSession( $ticket );
				break;
			case 'ticket':
				$result = $this->userTicket();
				break;
			default:
				// unsupported GET request
				throw new Exception( "GET Request command '$this->command' is not currently supported by this User API.", ErrorCodes::BAD_REQUEST );
				break;
		}

		return $result;
	}

	/**
	 * @return array
	 * @throws Exception
	 */
	public function actionPost()
	{
		$this->detectCommonParams();
		$data = Utilities::getPostDataAsArray();
		switch ( $this->command )
		{
			case 'session':
				$email = Utilities::getArrayValue( 'email', $data, '' );
				$password = Utilities::getArrayValue( 'password', $data, '' );
				//$password = Utilities::decryptPassword($password);
				$result = $this->userLogin( $email, $password );
				break;
			case 'register':
				$firstName = Utilities::getArrayValue( 'first_name', $data, '' );
				$lastName = Utilities::getArrayValue( 'last_name', $data, '' );
				$displayName = Utilities::getArrayValue( 'display_name', $data, '' );
				$email = Utilities::getArrayValue( 'email', $data, '' );
				$password = Utilities::getArrayValue( 'password', $data, '' );
				//$password = Utilities::decryptPassword($password);
				$result = $this->userRegister( $email, $password, $firstName, $lastName, $displayName );
				break;
			case 'confirm':
				$code = Utilities::getArrayValue( 'code', $_REQUEST, '' );
				if ( empty( $code ) )
				{
					$code = Utilities::getArrayValue( 'code', $data, '' );
				}
				$email = Utilities::getArrayValue( 'email', $_REQUEST, '' );
				if ( empty( $email ) )
				{
					$email = Utilities::getArrayValue( 'email', $data, '' );
				}
				if ( empty( $email ) && !empty( $code ) )
				{
					throw new Exception( "Missing required email or code for invitation.", ErrorCodes::BAD_REQUEST );
				}
				$newPassword = Utilities::getArrayValue( 'new_password', $data, '' );
				if ( empty( $newPassword ) )
				{
					throw new Exception( "Missing required fields 'new_password'.", ErrorCodes::BAD_REQUEST );
				}
				if ( !empty( $code ) )
				{
					$result = $this->passwordResetByCode( $code, $newPassword );
				}
				else
				{
{'fraction_non_alphanumeric': 0.1555124305045631, 'fraction_numerical': 0.0009440889541592364, 'mean_word_length': 2.5499906907466023, 'pattern_counts': {'":': 0, '<': 15, '<?xml version=': 0, '>': 230, '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': '5924178', 'n_tokens_mistral': 7237, 'n_tokens_neox': 5613, 'n_words': 1617}