-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
04f90b2
commit ced9bde
Showing
2 changed files
with
77 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
/** | ||
* This file is part of Hyperf. | ||
* | ||
* @link https://www.hyperf.io | ||
* @document https://hyperf.wiki | ||
* @contact [email protected] | ||
* @license https://github.com/hyperf/hyperf/blob/master/LICENSE | ||
*/ | ||
|
||
namespace Fan\SqlImport; | ||
|
||
use Hyperf\Database\Connectors\ConnectionFactory; | ||
use Psr\Container\ContainerInterface; | ||
|
||
class Privilege | ||
{ | ||
protected ConnectionFactory $factory; | ||
|
||
public function __construct(protected ContainerInterface $container) | ||
{ | ||
$this->factory = $this->container->get(ConnectionFactory::class); | ||
} | ||
|
||
public function createUser(string $name, string $host, string $password, array $config): bool | ||
{ | ||
$pdo = $this->factory->make($config)->getPdo(); | ||
|
||
$sql = sprintf("CREATE USER '%s'@'%s' IDENTIFIED BY '%s';", $name, $host, $password); | ||
|
||
if ($pdo->exec($sql) !== false) { | ||
$pdo->exec('FLUSH PRIVILEGES;'); | ||
return true; | ||
} | ||
|
||
return false; | ||
} | ||
|
||
public function grant(string $name, string $host, string $database, string $table, array $config): bool | ||
{ | ||
$pdo = $this->factory->make($config)->getPdo(); | ||
|
||
$sql = sprintf("GRANT ALL PRIVILEGES ON %s.%s TO '%s'@'%s';", $database, $table, $name, $host); | ||
|
||
if ($pdo->exec($sql) !== false) { | ||
$pdo->exec('FLUSH PRIVILEGES;'); | ||
return true; | ||
} | ||
|
||
return false; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters