Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 3 additions & 2 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -48,12 +48,13 @@
"require-dev": {
"pestphp/pest": "^1.0 | ^2.0",
"friendsofphp/php-cs-fixer": "^3.64",
"leafs/alchemy": "*"
"leafs/alchemy": "*",
"rector/rector": "^2.2"
},
"scripts": {
"alchemy": "./vendor/bin/alchemy setup",
"test": "./vendor/bin/alchemy setup --test",
"lint": "./vendor/bin/alchemy setup --lint",
"actions": "./vendor/bin/alchemy setup --actions"
}
}
}
15 changes: 15 additions & 0 deletions rector.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
<?php

declare(strict_types=1);

use Rector\Config\RectorConfig;

return RectorConfig::configure()
->withDowngradeSets(false, false, false, false, false, true)
->withFluentCallNewLine()
->withImportNames(true, true, true, true)
->withPaths([
__DIR__ . '/src',
__DIR__ . '/tests',
])
;
57 changes: 31 additions & 26 deletions src/Auth.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,17 @@

namespace Leaf;

use Exception;
use Firebase\JWT\JWT;
use Firebase\JWT\Key;
use Leaf\Auth\Config;
use Leaf\Auth\User;
use Leaf\Exception\General;
use Leaf\Helpers\Password;
use Leaf\Http\Session;
use League\OAuth2\Client\Provider\Google;
use PDO;
use Throwable;

/**
* Leaf Simple Auth
Expand Down Expand Up @@ -55,31 +60,31 @@ public function __construct()
});

$this->middleware('is', function ($role) {
\Leaf\Exception\General::error(
General::error(
'404',
'<p>The page you are looking for could not be found.</p>',
403
);
});

$this->middleware('isNot', function () {
\Leaf\Exception\General::error(
General::error(
'404',
'<p>The page you are looking for could not be found.</p>',
403
);
});

$this->middleware('can', function () {
\Leaf\Exception\General::error(
General::error(
'404',
'<p>The page you are looking for could not be found.</p>',
403
);
});

$this->middleware('cannot', function () {
\Leaf\Exception\General::error(
General::error(
'404',
'<p>The page you are looking for could not be found.</p>',
403
Expand Down Expand Up @@ -141,10 +146,10 @@ public function autoConnect(array $pdoOptions = [])
/**
* Pass in db connection instance directly
*
* @param \PDO $connection A connection instance of your db
* @param PDO $connection A connection instance of your db
* @return $this;
*/
public function dbConnection(\PDO $connection)
public function dbConnection(PDO $connection)
{
$this->db = new Db();
$this->db->connection($connection);
Expand Down Expand Up @@ -174,7 +179,7 @@ public function withGoogle(
$options['redirectUri'] = _env('APP_URL') . '/auth/google/callback';
}

$this->withProvider($clientName, new \League\OAuth2\Client\Provider\Google(array_merge([
$this->withProvider($clientName, new Google(array_merge([
'clientId' => $clientId,
'clientSecret' => $clientSecret,
'redirectUri' => $options['redirectUri'],
Expand Down Expand Up @@ -283,8 +288,8 @@ public function login(array $credentials): bool
$this->errorsArray['auth'] = Config::get('messages.loginParamsError');
return false;
}
} catch (\Throwable $th) {
throw new \Exception($th->getMessage());
} catch (Throwable $th) {
throw new Exception($th->getMessage());
}

if ($passwordKey !== false) {
Expand Down Expand Up @@ -350,8 +355,8 @@ public function register(array $userData): bool
$this->errorsArray = array_merge($this->errorsArray, $this->db->errors());
return false;
}
} catch (\Throwable $th) {
throw new \Exception($th->getMessage());
} catch (Throwable $th) {
throw new Exception($th->getMessage());
}

$user = $this->db->select($table)->where($userData)->first();
Expand Down Expand Up @@ -420,8 +425,8 @@ public function update(array $userData): bool
$this->errorsArray = array_merge($this->errorsArray, $this->db->errors());
return false;
}
} catch (\Throwable $th) {
throw new \Exception($th->getMessage());
} catch (Throwable $th) {
throw new Exception($th->getMessage());
}

if (Config::get('session')) {
Expand Down Expand Up @@ -483,8 +488,8 @@ public function updatePassword(string $oldPassword, string $newPassword): bool
$this->errorsArray = array_merge($this->errorsArray, $this->db->errors());
return false;
}
} catch (\Throwable $th) {
throw new \Exception($th->getMessage());
} catch (Throwable $th) {
throw new Exception($th->getMessage());
}

$this->user->{$passwordKey} = $newPassword;
Expand Down Expand Up @@ -592,8 +597,8 @@ public function createUserFor($userData)
$this->errorsArray = array_merge($this->errorsArray, $this->db->errors());
return false;
}
} catch (\Throwable $th) {
throw new \Exception($th->getMessage());
} catch (Throwable $th) {
throw new Exception($th->getMessage());
}

$user = $this->db->select($table)->where($userData)->first();
Expand Down Expand Up @@ -690,8 +695,8 @@ public function user()
$this->errorsArray = $this->db->errors();
return null;
}
} catch (\Throwable $th) {
throw new \Exception($th->getMessage());
} catch (Throwable $th) {
throw new Exception($th->getMessage());
}

return $this->user = (new User(
Expand Down Expand Up @@ -736,8 +741,8 @@ public function tokens()
*/
public function middleware(string $middleware, callable $callback)
{
if (!class_exists(\Leaf\App::class)) {
throw new \Exception('This feature is only available for Leaf apps');
if (!class_exists(App::class)) {
throw new Exception('This feature is only available for Leaf apps');
}

if ($middleware === 'auth.required') {
Expand Down Expand Up @@ -833,7 +838,7 @@ public function parseToken()
$bearerToken,
new Key(Config::get('token.secret'), 'HS256')
);
} catch (\Throwable $th) {
} catch (Throwable $th) {
$this->errorsArray['token'] = $th->getMessage();
return null;
}
Expand Down Expand Up @@ -875,7 +880,7 @@ public function verifyToken(string $token, ?string $purpose = null)
}

return $user;
} catch (\Throwable $th) {
} catch (Throwable $th) {
$this->errorsArray['token'] = $th->getMessage();
return null;
}
Expand All @@ -894,13 +899,13 @@ public function db()
protected function checkDbConnection(): void
{
if (!$this->db && function_exists('db')) {
if (db()->connection() instanceof \PDO || db()->autoConnect()) {
if (db()->connection() instanceof PDO || db()->autoConnect()) {
$this->db = db();
}
}

if (!$this->db) {
throw new \Exception('You need to connect to your database first');
throw new Exception('You need to connect to your database first');
}
}

Expand All @@ -916,7 +921,7 @@ protected function getFromSession($value)
protected function sessionCheck()
{
if (!Config::get('session')) {
throw new \Exception('Turn on sessions to use this feature.');
throw new Exception('Turn on sessions to use this feature.');
}
}

Expand Down
20 changes: 11 additions & 9 deletions src/Auth/Model.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

namespace Leaf\Auth;

use Leaf\Date;
use Leaf\Db;
use PDOStatement;

/**
Expand All @@ -27,7 +29,7 @@ class Model
/**
* DB connection
*/
protected \Leaf\Db $db;
protected Db $db;

/**
* User data to save
Expand All @@ -53,7 +55,7 @@ public function create(array $data): ?PDOStatement
$data['user_id'] = $this->user->id();

if (Config::get('timestamps')) {
$now = (new \Leaf\Date())->tick()->format(Config::get('timestamps.format'));
$now = (new Date())->tick()->format(Config::get('timestamps.format'));
$data['created_at'] = $now;
$data['updated_at'] = $now;
}
Expand All @@ -66,11 +68,11 @@ public function create(array $data): ?PDOStatement
*
* @param array $data Data to be updated
*
* @return \Leaf\Db
* @return Db
*/
public function update(array $data): \Leaf\Db
public function update(array $data): Db
{
$data['updated_at'] = (new \Leaf\Date())->tick()->format(Config::get('timestamps.format'));
$data['updated_at'] = (new Date())->tick()->format(Config::get('timestamps.format'));

return $this->db->update($this->table)
->params($data)
Expand All @@ -80,9 +82,9 @@ public function update(array $data): \Leaf\Db
/**
* Delete a model resource
*
* @return \Leaf\Db
* @return Db
*/
public function delete(): \Leaf\Db
public function delete(): Db
{
return $this->db->delete($this->table)
->where('user_id', $this->user->id());
Expand All @@ -93,9 +95,9 @@ public function delete(): \Leaf\Db
*
* @param string $columns Columns to search by separated by commas or *
*
* @return \Leaf\Db
* @return Db
*/
public function table($columns = '*'): \Leaf\Db
public function table($columns = '*'): Db
{
return $this->db->select($this->table, $columns)
->where('user_id', $this->user->id());
Expand Down
15 changes: 9 additions & 6 deletions src/Auth/User.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,11 @@

namespace Leaf\Auth;

use Exception;
use Firebase\JWT\JWT;
use Leaf\Db;
use Leaf\Http\Session;
use Throwable;

/**
* Auth User
Expand All @@ -20,7 +23,7 @@ class User

/**
* Internal instance of Leaf database
* @var \Leaf\DB
* @var Db
*/
protected $db;

Expand Down Expand Up @@ -69,7 +72,7 @@ public function __construct($data, $session = true)
$sessionLifetime = strtotime($sessionLifetime);

if (!$sessionLifetime) {
throw new \Exception('Invalid session lifetime');
throw new Exception('Invalid session lifetime');
}
} else {
$sessionLifetime = time() + $sessionLifetime;
Expand Down Expand Up @@ -209,7 +212,7 @@ public function verifyEmail(): bool
->execute();

return true;
} catch (\Throwable $th) {
} catch (Throwable $th) {
return false;
}
}
Expand Down Expand Up @@ -243,7 +246,7 @@ public function get()

/**
* Set user db instance
* @param \Leaf\Db $db
* @param Db $db
* @return User
*/
public function setDb($db)
Expand Down Expand Up @@ -293,14 +296,14 @@ public function __unset($name)
*
* @param mixed $method The table to relate to
* @param mixed $args
* @throws \Exception
* @throws Exception
*
* @return Model
*/
public function __call($method, $args)
{
if (!class_exists('Leaf\App')) {
throw new \Exception('Relations are only available in Leaf apps.');
throw new Exception('Relations are only available in Leaf apps.');
}

return (new Model([
Expand Down
4 changes: 3 additions & 1 deletion src/Auth/UsesRoles.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

namespace Leaf\Auth;

use Throwable;

/**
* Functionality for user permissions
* ----
Expand Down Expand Up @@ -46,7 +48,7 @@ public function assign($role): bool
])
->where(Config::get('id.key'), $this->data[Config::get('id.key')])
->execute();
} catch (\Throwable $th) {
} catch (Throwable $th) {
return false;
}

Expand Down
Loading