Ejemplo n.º 1
0
User::applyFilter('save', function($self, $params, $chain) {
    // TODO: below
    return $chain->next($self, $params, $chain);
    
	
	// Do this except for those with a facebook uid, the FB PHP SDK takes care of that
	if(!isset($params['data']['facebook_uid'])) {
	
		/*if(!empty($params['data']['profile_pic'])) {
			$asset = Asset::create();
			// Technically the 'model' field is not required because ids are universally unique, but it's easier for to code if we know which model
			$asset->save(array('file' => $params['data']['profile_pic'], 'model' => 'User', 'parent_id' => $params['data']['_id']));
			$asset_data = $asset->data();
			$params['data']['profile_pics'][] = $asset_data['_id'];
		}*/
		
		// Set created, modified, and pretty url (slug)
		if (!$params['entity']->exists()) {
			if(Validator::rule('moreThanFive', $params['data']['password']) === true) {
				$params['data']['password'] = String::hash($params['data']['password']); // will be sha512
			}
			// Unique E-mail validation ONLY upon new record creation
			if(Validator::rule('uniqueEmail', $params['data']['email']) === false) {
				$params['data']['email'] = ''; 
			}
			
		} else {
			// If the fields password and password_confirm both exist, then validate the password field too
			if((isset($params['data']['password'])) && (isset($params['data']['password_confirm']))) {
				if(Validator::rule('moreThanFive', $params['data']['password']) === true) {
					$params['data']['password'] = String::hash($params['data']['password']); // will be sha512
				}
			}
			
			// If the new_email field was passed, the user is requesting to update their e-mail, we will set it and send an email to allow them to confirm, once confirmed it will be changed
			if(isset($params['data']['new_email'])) {
				// Unique E-mail validation
				if((Validator::rule('uniqueEmail', $params['data']['new_email']) === false) || (Validator::isEmail($params['data']['new_email']) === false)) {
					// Invalidate
					$params['data']['new_email'] = '';
				} else {
					$params['data']['approval_code'] = Util::unique_string(array('hash' => 'md5'));
					Email::changeUserEmail(array('first_name' => $params['data']['first_name'], 'last_name' => $params['data']['last_name'], 'to' => $params['data']['new_email'], 'approval_code' => $params['data']['approval_code']));
				}
			}
		}
	
	}
	
	//$data = array($params['entity']->file);
	//Asset::save($data);
	
	return $chain->next($self, $params, $chain);
});