/** * @return array|bool * @throws Exception * @author Erik Aybar */ public static function getCurrentUser() { if (!static::checkIfLoggedIn()) { return false; } $user_id = $_SESSION['user_id']; $user = User::getOne($user_id); if (!$user) { throw new Exception("User not found using session user_id {$user_id}. Bad!"); } return $user; }
<?php require_once $_SERVER['DOCUMENT_ROOT'] . '/bootstrap.php'; $rules = ['email' => ["email"], 'password' => ["not_empty"]]; $validator = new \MyClasses\Validation\Validator(); $validator->validate($rules, $_POST); $validator->redirectWithErrorsIfFailed('/users/login.php'); $user = \MyClasses\Models\User::getOneBy('email', $_POST['email']); $hashed = $user['encrypted_password']; $password_is_correct = password_verify($_POST['password'], $hashed); if ($password_is_correct) { \MyClasses\Auth\AuthMaster::logUserInUsingId($user['id']); redirect_user('/users/index.php', "Log in success. Congratulations, {$user['first_name']}!"); } else { redirect_user('/users/login.php', "Wrong password! Try again..."); }
<?php require_once $_SERVER['DOCUMENT_ROOT'] . '/bootstrap.php'; $rules = ['first_name' => ['not_empty'], 'last_name' => ['not_empty'], 'email' => ['not_empty'], 'password' => ['not_empty'], 'password_confirmation' => ['not_empty']]; $validator = new \MyClasses\Validation\Validator(); $validator->validate($rules, $_POST); $validator->redirectWithErrorsIfFailed('/users/new.php'); if ($_POST['password'] != $_POST['password_confirmation']) { redirect_user("/users/new.php", "Whoops. Your password confirmation didn't match..."); } $encrypted_password = password_hash($_POST['password'], PASSWORD_BCRYPT); $user_create_data = ['first_name' => $_POST['first_name'], 'last_name' => $_POST['last_name'], 'email' => $_POST['email'], 'encrypted_password' => $encrypted_password]; $users_id = \MyClasses\Models\User::create($user_create_data); $user = \MyClasses\Models\User::getOne($users_id); \MyClasses\Auth\AuthMaster::logUserInUsingId($user['id']); redirect_user('/users/show.php?id=' . $users_id, "Welcome, {$user['first_name']}!");
<?php require_once $_SERVER['DOCUMENT_ROOT'] . '/bootstrap.php'; if ($_SERVER['REQUEST_METHOD'] != "POST") { redirect_user('/users/index.php', "Bad method. Bad user!"); } $user_id = $_POST['id']; $user = \MyClasses\Models\User::getOne($user_id); $destroyed = \MyClasses\Models\User::destroy($user_id); redirect_user('/users/index.php', "You killed {$user['first_name']}!");
<?php require_once $_SERVER['DOCUMENT_ROOT'] . '/bootstrap.php'; if (!isset($_POST['id'])) { redirect_user('/users/index.php', 'No user found for ID ... or you didn\'t supply one!'); } $user_id = $_POST['id']; $user = \MyClasses\Models\User::getOne($user_id); // Get form data $validate_fields = ['first_name' => "/\\w+/", 'last_name' => "/\\w+/", 'age' => "/\\d+/"]; foreach ($validate_fields as $key => $pattern) { if (!preg_match($pattern, $_POST[$key])) { redirect_user("/users/edit.php?id=" . $user_id, "Whoops. Looks like you forgot to fill in \"{$key}\"!"); } } $first_name = $_POST['first_name']; $last_name = $_POST['last_name']; $age = $_POST['age']; // Update User $success = \MyClasses\Models\User::update($user_id, compact('first_name', 'last_name', 'age')); // Redirect user $success = $success ? "YES" : json_encode($pdo_connection->errorInfo()); redirect_user("/users/edit.php?id=" . $user_id, "Updated... whatever. Success: " . $success);
<?php require_once $_SERVER['DOCUMENT_ROOT'] . '/bootstrap.php'; $page['title'] = 'Users'; echo get_partial('header.php', ['page' => $page]); $wheres = []; $order_bys = []; if (!empty($_GET['order_by'])) { $order_bys[] = $_GET['order_by']; } $users = \MyClasses\Models\User::getAll($wheres, $order_bys); $existing_query_params = $_GET; ?> <div class="row"> <div class="col-sm-4"> <h1>All Users</h1> </div> <div class="col-sm-8"> <form action=""> <div class="row" style="padding-top: 40px;"> <div class="col-sm-2"> <?php if (!empty($_GET['order_by'])) { ?> <input type="hidden" name="order_by" value="<?php echo $_GET['order_by']; ?> "/> <?php }