<commit_before><?php defined('SYSPATH') or die('No direct access allowed.');

/**
 * CrowdmapID authorization library. Handles login of users when the system's
 * authentication provider is CrowdmapID
 *
 * PHP version 5
 * LICENSE: This source file is subject to the AGPL license 
 * that is available through the world-wide-web at the following URI:
 * http://www.gnu.org/licenses/agpl.html
 * @author      <NAME> <<EMAIL>> 
 * @package     PingApp
 * @copyright   Ushahidi - http://www.ushahidi.com
 * @license     http://www.gnu.org/licenses/gpl.html GNU General Public License (GPL) 
 */
class Pingapp_Auth_CrowdmapID extends Kohana_Auth_ORM { 
	
	/**
	 * Logs a user in.
	 *
	 * @param   string   email
	 * @param   string   password
	 * @param   boolean  enable autologin
	 * @return  boolean
	 */
	protected function _login($email, $password, $remember) 
	{
		$crowdmapid_api = CrowdmapID_API::instance();
		
		// Fallback to local auth if user is in the exemption list
		if (in_array($email, Kohana::$config->load('crowdmapid.auth_exempt')))
			return parent::_login($email, $password, $remember);
		
		// Check if the email is registered on CrowdmapID
		if ($crowdmapid_api->is_registered($email))
		{
			// Success! Proceed to sign in into CrowdmapID
			$login_response = $crowdmapid_api->signin($email, $password);
			
			if ($login_response AND $login_response['status'])
			{
				// Get the user object that matches the provided email and CrowdmapID
				$user = ORM::factory('User')
				    ->where('email', '=', $email)
				    ->where('crowdmap_id', '=', $login_response['user_id'])
				    ->find();
							 
				// User does not exist locally but authenticates via CrowdmapID, create user
				if ( ! $user->loaded())
				{
					// Check if the email is already registered locally
					// If so, this will simply append a riverid
					$user = ORM::factory('User')
					            ->where('email', '=', $email)
					            ->find();
					
					$user->username = $user->email = $email;
					$user->riverid = $login_response['user_id'];
					$user->save();
					
					// Allow the user be able to login immediately
					$login_role = ORM::factory('Role',array('name'=>'login'));
					
					if ( ! $user->has('roles', $login_role))
					{
						$user->add('roles', $login_role);
					}
				} 
				
				// User exists locally and authenticates via CrowdmapID so complete the login
				if ($user->has('roles', ORM::factory('Role', array('name' => 'login'))))
				{
					if ($remember === TRUE)
					{
						// Token data
						$data = array(
							'user_id'    => $user->id,
							'expires'    => time() + $this->_config['lifetime'],
							'user_agent' => sha1(Request::$user_agent),
						);<commit_msg>Assign member and login roles<commit_after><?php defined('SYSPATH') or die('No direct access allowed.');

/**
 * CrowdmapID authorization library. Handles login of users when the system's
 * authentication provider is CrowdmapID
 *
 * PHP version 5
 * LICENSE: This source file is subject to the AGPL license 
 * that is available through the world-wide-web at the following URI:
 * http://www.gnu.org/licenses/agpl.html
 * @author      <NAME> <<EMAIL>> 
 * @package     PingApp
 * @copyright   Ushahidi - http://www.ushahidi.com
 * @license     http://www.gnu.org/licenses/gpl.html GNU General Public License (GPL) 
 */
class Pingapp_Auth_CrowdmapID extends Kohana_Auth_ORM { 
	
	/**
	 * Logs a user in.
	 *
	 * @param   string   email
	 * @param   string   password
	 * @param   boolean  enable autologin
	 * @return  boolean
	 */
	protected function _login($email, $password, $remember) 
	{
		$crowdmapid_api = CrowdmapID_API::instance();
		
		// Fallback to local auth if user is in the exemption list
		if (in_array($email, Kohana::$config->load('crowdmapid.auth_exempt')))
			return parent::_login($email, $password, $remember);
		
		// Check if the email is registered on CrowdmapID
		if ($crowdmapid_api->is_registered($email))
		{
			// Success! Proceed to sign in into CrowdmapID
			$login_response = $crowdmapid_api->signin($email, $password);
			
			if ($login_response AND $login_response['status'])
			{
				// Get the user object that matches the provided email and CrowdmapID
				$user = ORM::factory('User')
				    ->where('email', '=', $email)
				    ->find();
							 
				// User does not exist locally but authenticates via CrowdmapID, create user
				if ( ! $user->loaded())
				{
					// Check if the email is already registered locally
					// If so, this will simply append a riverid
					$user = ORM::factory('User')
					            ->where('email', '=', $email)
					            ->find();
					
					$user->username = $user->email = $email;
					$user->save();
					
					try
					{
						// Allow the user be able to login immediately
						$user_roles = ORM::factory('Role')
						    ->where('name', 'IN', array('login', 'member'))
						    ->find_all();
					
						$role_ids = array();
						foreach ($user_roles as $role)
						{
							$role_ids[] = $role->id;
						}
					
						if ( ! $user->has('roles', $role_ids))
						{
							$user->add('roles', $role_ids);
						}
					}
					catch (Exception $e)
					{
						// An error has occurred, delete the user
						$user->delete();
						
						// Log the error
						Kohana::$log->add(Log::ERROR, $e->getMessage());
						
						return FALSE;
					}
				} 
				
				// User exists locally and authenticates via CrowdmapID so complete the login
				if ($user->has('roles', ORM::factory('Role', array('name' => 'login'))))
				{
					if ($remember === TRUE)
					{
						// Token data
						$data = array(
							'user_id'    => $user->id,
							'expires'    => time() + $this->_config['lifetime'],
							'user_agent' => sha1(Request::$user_agent),
						);
{'fraction_non_alphanumeric': 0.14409421544856252, 'fraction_numerical': 0.0006927606511950121, 'mean_word_length': 2.6389413988657844, 'pattern_counts': {'":': 0, '<': 11, '<?xml version=': 0, '>': 58, 'https://': 0, 'lorem ipsum': 0, 'www.': 6, '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': '3623495', 'n_tokens_mistral': 2293, 'n_tokens_neox': 1768, 'n_words': 496}