pierre-fromager /
nymfonya
| 1 | <?php |
||
| 2 | |||
| 3 | declare(strict_types=1); |
||
| 4 | |||
| 5 | namespace App\Model; |
||
| 6 | |||
| 7 | use Nymfonya\Component\Container; |
||
| 8 | use Nymfonya\Component\Config; |
||
| 9 | use App\Component\Auth\AuthInterface; |
||
| 10 | use App\Model\AbstractSearch; |
||
| 11 | use App\Component\Crypt; |
||
| 12 | |||
| 13 | /** |
||
| 14 | * Class App\Model\Accounts |
||
| 15 | * |
||
| 16 | * Provides account list from file accounts.csv. |
||
| 17 | */ |
||
| 18 | class Accounts extends AbstractSearch implements AuthInterface |
||
| 19 | { |
||
| 20 | const _NAME = 'name'; |
||
| 21 | const _STATUS = 'status'; |
||
| 22 | const _ROLE = 'role'; |
||
| 23 | const PATH_ASSETS_MODEL = '/../assets/model/'; |
||
| 24 | const ACCOUNTS_FILENAME = '/accounts.csv'; |
||
| 25 | const FIELD_SEPARATOR = ','; |
||
| 26 | const FILTER_ALL = '/^(.*),(.*),(.*),(.*),(.*),(.*)/'; |
||
| 27 | |||
| 28 | /** |
||
| 29 | * config |
||
| 30 | * |
||
| 31 | * @var Config |
||
| 32 | */ |
||
| 33 | protected $config; |
||
| 34 | |||
| 35 | /** |
||
| 36 | * instanciate |
||
| 37 | * |
||
| 38 | */ |
||
| 39 | 11 | public function __construct(Container $container) |
|
| 40 | { |
||
| 41 | 11 | parent::__construct($container); |
|
| 42 | 11 | $this->config = $this->getService(Config::class); |
|
| 43 | 11 | $this->init(); |
|
| 44 | 11 | return $this; |
|
| 45 | } |
||
| 46 | |||
| 47 | /** |
||
| 48 | * auth |
||
| 49 | * |
||
| 50 | * @return array |
||
| 51 | */ |
||
| 52 | 3 | public function auth(string $login, string $password): array |
|
| 53 | { |
||
| 54 | $filter = |
||
| 55 | '/^(.*),' |
||
| 56 | . '(.*),' |
||
| 57 | 3 | . '(' . $login . '),' |
|
| 58 | 3 | . '(.*),' |
|
| 59 | 3 | . '(.*),' |
|
| 60 | 3 | . '(.*)/'; |
|
| 61 | 3 | $this->setFilter($filter)->readFromStream(); |
|
| 62 | 3 | $result = $this->get(); |
|
| 63 | 3 | if (empty($result)) { |
|
| 64 | 1 | return []; |
|
| 65 | } |
||
| 66 | 2 | $user = $result[0]; |
|
| 67 | 2 | $crypt = new Crypt($this->config); |
|
| 68 | 2 | if ($password == $crypt->decrypt($user[self::_PASSWORD], true)) { |
|
| 69 | 1 | return $user; |
|
| 70 | } |
||
| 71 | 1 | unset($crypt); |
|
| 72 | 1 | return []; |
|
| 73 | } |
||
| 74 | |||
| 75 | /** |
||
| 76 | * getById |
||
| 77 | * |
||
| 78 | * @return array |
||
| 79 | */ |
||
| 80 | 1 | public function getById(int $id): array |
|
| 81 | { |
||
| 82 | $filter = |
||
| 83 | 1 | '/^(' . $id . '),' |
|
| 84 | 1 | . '(.*),' |
|
| 85 | 1 | . '(.*),' |
|
| 86 | 1 | . '(.*),' |
|
| 87 | 1 | . '(.*),' |
|
| 88 | 1 | . '(.*)/'; |
|
| 89 | 1 | $this->setFilter($filter)->readFromStream(); |
|
| 90 | 1 | $result = $this->get(); |
|
| 91 | 1 | if (empty($result)) { |
|
| 92 | 1 | return []; |
|
| 93 | } |
||
| 94 | 1 | $user = $result[0]; |
|
| 95 | 1 | $crypt = new Crypt($this->config); |
|
| 96 | 1 | $clearPassword = $crypt->decrypt($user[self::_PASSWORD], true); |
|
| 97 | 1 | $user[self::_PASSWORD] = $clearPassword; |
|
| 98 | 1 | return $user; |
|
| 99 | } |
||
| 100 | |||
| 101 | /** |
||
| 102 | * return csv content as array |
||
| 103 | * |
||
| 104 | * @return array |
||
| 105 | */ |
||
| 106 | 1 | public function toArray(): array |
|
| 107 | { |
||
| 108 | 1 | return array_map( |
|
| 109 | 1 | 'str_getcsv', |
|
| 110 | 1 | file($this->getAccountsFilename()) |
|
|
0 ignored issues
–
show
Bug
introduced
by
Loading history...
|
|||
| 111 | ); |
||
| 112 | } |
||
| 113 | |||
| 114 | /** |
||
| 115 | * init |
||
| 116 | * |
||
| 117 | * @param string $assetsPath |
||
| 118 | * @return AbstractSearch |
||
| 119 | */ |
||
| 120 | 1 | protected function init(): AbstractSearch |
|
| 121 | { |
||
| 122 | 1 | $this->setFilename($this->getAccountsFilename()); |
|
| 123 | 1 | $this->createFile($this->filename); |
|
| 124 | 1 | $this->setFilter(self::FILTER_ALL); |
|
| 125 | 1 | $this->setSeparator(self::FIELD_SEPARATOR); |
|
| 126 | 1 | return $this; |
|
| 127 | } |
||
| 128 | |||
| 129 | /** |
||
| 130 | * return csv file accounts filename |
||
| 131 | * |
||
| 132 | * @return string |
||
| 133 | */ |
||
| 134 | 1 | protected function getAccountsFilename(): string |
|
| 135 | { |
||
| 136 | 1 | return realpath( |
|
| 137 | 1 | $this->config->getPath() . self::PATH_ASSETS_MODEL |
|
| 138 | 1 | ) . self::ACCOUNTS_FILENAME; |
|
| 139 | } |
||
| 140 | |||
| 141 | /** |
||
| 142 | * add account item to stack |
||
| 143 | * |
||
| 144 | * @param array $data |
||
| 145 | * @return AbstractSearch |
||
| 146 | */ |
||
| 147 | 1 | protected function setItem(array $data): AbstractSearch |
|
| 148 | { |
||
| 149 | 1 | $this->datas[] = [ |
|
| 150 | 1 | self::_ID => $data[0], |
|
| 151 | 1 | self::_NAME => $data[1], |
|
| 152 | 1 | self::_EMAIL => $data[2], |
|
| 153 | 1 | self::_PASSWORD => $data[3], |
|
| 154 | 1 | self::_STATUS => $data[4], |
|
| 155 | 1 | self::_ROLE => $data[5] |
|
| 156 | ]; |
||
| 157 | 1 | return $this; |
|
| 158 | } |
||
| 159 | |||
| 160 | /** |
||
| 161 | * create csv file account from config accounts setting |
||
| 162 | * |
||
| 163 | * @param string $filename |
||
| 164 | * @return AbstractSearch |
||
| 165 | */ |
||
| 166 | 1 | protected function createFile(string $filename): AbstractSearch |
|
| 167 | { |
||
| 168 | 1 | if (!file_exists($filename)) { |
|
| 169 | 1 | $crypt = new Crypt($this->config); |
|
| 170 | 1 | $accounts = $this->config->getSettings(Config::_ACCOUNTS); |
|
| 171 | $accounts = array_map(function ($acc) use ($crypt) { |
||
| 172 | 1 | $acc[self::_PASSWORD] = $crypt->encrypt( |
|
| 173 | 1 | $acc[self::_PASSWORD], |
|
| 174 | 1 | true |
|
| 175 | ); |
||
| 176 | 1 | return $acc; |
|
| 177 | 1 | }, $accounts); |
|
| 178 | 1 | $handler = fopen($filename, 'w'); |
|
| 179 | 1 | $error = (false === $handler); |
|
| 180 | 1 | if ($error === false) { |
|
| 181 | 1 | foreach ($accounts as $record) { |
|
| 182 | 1 | fputcsv($handler, array_values($record)); |
|
| 183 | } |
||
| 184 | 1 | fclose($handler); |
|
| 185 | } |
||
| 186 | 1 | unset($handler, $accounts, $crypt); |
|
| 187 | } |
||
| 188 | 1 | return $this; |
|
| 189 | } |
||
| 190 | } |
||
| 191 |