PHP code example of germania-kg / user-profiles
1. Go to this page and download the library: Download germania-kg/user-profiles library . Choose the download type require .
2. Extract the ZIP file and open the index.php.
3. Add this code to the index.php.
<?php
require_once('vendor/autoload.php');
/* Start to develop here. Best regards https://php-download.com/ */
germania-kg / user-profiles example snippets
use Germania\UserProfiles\RegisterUserWrapper;
use Germania\UserProfiles\Exceptions\RegisterUserException;
$pdo = new PDO( ... );
$hash = function() { return ... ; };
$rnd = function() { return ... ; };
$logger = new Monolog();
$users_table = 'users';
$users_roles_table = 'users_roles';
// Setup callable
$register = new RegisterUserWrapper( $pdo, $hash , $rnd, $users_table, $users_roles_table, $logger);
// User data
$user_data = [
'first_name' => 'John',
'last_name' => 'Doe',
'display_name' => 'John Doe',
'email' => '[email protected] ',
'login' => '[email protected] '
];
$roles = [1, 22];
// Store new user
try {
$user_id = $register( $user_data, $roles );
}
catch (RegisterUserException $e) {
echo "Failed: ", $e->getMessage();
}
use Germania\UserProfiles\PdoUsernameChecker;
use Germania\UserProfiles\Exceptions\LoginNameNotAvailableException;
$pdo = new PDO( ... );
$logger = new Monolog();
$table = 'users';
try {
$lookup = new PdoUsernameChecker( $pdo, $logger, $table);
$available = $lookup( 'johndoe_224' );
}
catch (LoginNameNotAvailableException $e) {
// Login name not available, someone else using it?
}
use Germania\UserProfiles\PdoInsertNewUser;
use Germania\UserProfiles\Exceptions\InsertUserException;
$pdo = new PDO( ... );
$logger = new Monolog();
$table = 'users';
try {
$inserter = new PdoInsertNewUser( $pdo, $logger, $table);
$new_id = $inserter([
'first_name' => 'John',
'last_name' => 'Doe',
'display_name' => 'John Doe',
'email' => '[email protected] ',
'login' => '[email protected] '
]);
}
catch (InsertUserException $e) {
// Hmmm. Could not insert new User?
// Maybe some fields missing?
}
use Germania\UserProfiles\PdoPasswordSetter;
use Germania\UserProfiles\Exceptions\SetPasswordException;
$pdo = new PDO( ... );
$hash = function() { return 'ABCDEF'; };
$logger = new Monolog();
$table = 'users';
$user = 42;
try {
$setter = new PdoPasswordSetter( $pdo, $hash, $logger, $table);
$result = $setter( 42, 'top_secret' );
}
catch (SetPasswordException $e) {
// Could not change user's apssword?!
}
use Germania\UserProfiles\PdoApiKeySetter;
use Germania\UserProfiles\Exceptions\SetApiKeyException;
$pdo = new PDO( ... );
$rnd = function() { return 'ABCDEF'; };
$logger = new Monolog();
$table = 'users';
try {
$setter = new PdoApiKeySetter( $pdo, $random_gen, $logger, $table);
$result = $setter( 42 );
}
catch (SetApiKeyException $e) {
// Could not set new API key.
}
use Germania\UserProfiles\PdoSetActiveState;
use Germania\UserProfiles\Exceptions\SetActiveStateException;
$pdo = new PDO( ... );
$hash = function() { return 'ABCDEF'; };
$logger = new Monolog();
$table = 'users';
$user = 42;
try {
$setter = new PdoSetActiveState( $pdo, $logger, $table);
$result = $setter( 42, PdoSetActiveState::ACTIVE );
$result = $setter( 42, PdoSetActiveState::INACTIVE );
}
catch (SetActiveStateException $e) {
// Could not change active state?!
}
use Germania\UserProfiles\PdoProfileUpdater;
use Germania\UserProfiles\Exceptions\UpdateProfileException;
$pdo = new PDO( ... );
$logger = new Monolog();
$table = 'users';
$user = 42;
try {
$updater = new PdoProfileUpdater( $pdo, $logger, $table);
$result = $updater( $user, [
'first_name' => 'John',
'last_name' => 'Doe',
'display_name' => 'John Doe',
'email' => '[email protected] ',
'login_name' => '[email protected] '
]);
}
catch (UpdateProfileException $e) {
// Could not update the user's profile
}
use Germania\UserProfiles\PdoCredentialsValidator;
$pdo = new PDO( ... );
$verifier = function() { return false; };
$logger = new Monolog();
$table = 'users';
$checker = new PdoCredentialsValidator( $pdo, $verifier, $logger, $table);
$result = $checker( '[email protected] ', 'take_this_secret' );
if ($result) {
// valid
} else {
// Password and/or Login name are invalid!
}