|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace App\Model; |
|
4
|
|
|
|
|
5
|
|
|
use App\Container; |
|
6
|
|
|
use App\Config; |
|
7
|
|
|
use App\Model\AbstractSearch; |
|
8
|
|
|
use App\Tools\Crypt; |
|
9
|
|
|
|
|
10
|
|
|
/** |
|
11
|
|
|
* Class App\Model\Accounts |
|
12
|
|
|
* |
|
13
|
|
|
* Provides account list. |
|
14
|
|
|
*/ |
|
15
|
|
|
class Accounts extends AbstractSearch |
|
16
|
|
|
{ |
|
17
|
|
|
const _ID = 'id'; |
|
18
|
|
|
const _NAME = 'name'; |
|
19
|
|
|
const _EMAIL = 'email'; |
|
20
|
|
|
const _PASSWORD = 'password'; |
|
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
|
|
|
public function __construct(Container $container) |
|
40
|
|
|
{ |
|
41
|
|
|
parent::__construct($container); |
|
42
|
|
|
$this->config = $this->getService(\App\Config::class); |
|
43
|
|
|
$this->init(); |
|
44
|
|
|
return $this; |
|
45
|
|
|
} |
|
46
|
|
|
|
|
47
|
|
|
/** |
|
48
|
|
|
* init |
|
49
|
|
|
* |
|
50
|
|
|
* @param string $assetsPath |
|
51
|
|
|
* @return AbstractSearch |
|
52
|
|
|
*/ |
|
53
|
|
|
protected function init(): AbstractSearch |
|
54
|
|
|
{ |
|
55
|
|
|
$this->setFilename($this->getAccountsFilename()); |
|
56
|
|
|
$this->createFile($this->filename); |
|
57
|
|
|
$this->setFilter(self::FILTER_ALL); |
|
58
|
|
|
$this->setSeparator(self::FIELD_SEPARATOR); |
|
59
|
|
|
return $this; |
|
60
|
|
|
} |
|
61
|
|
|
|
|
62
|
|
|
/** |
|
63
|
|
|
* return csv file accounts filename |
|
64
|
|
|
* |
|
65
|
|
|
* @return string |
|
66
|
|
|
*/ |
|
67
|
|
|
protected function getAccountsFilename(): string |
|
68
|
|
|
{ |
|
69
|
|
|
return realpath( |
|
70
|
|
|
$this->config->getPath() . self::PATH_ASSETS_MODEL |
|
71
|
|
|
) . self::ACCOUNTS_FILENAME; |
|
72
|
|
|
} |
|
73
|
|
|
|
|
74
|
|
|
/** |
|
75
|
|
|
* add account item to stack |
|
76
|
|
|
* |
|
77
|
|
|
* @param array $data |
|
78
|
|
|
* @return AbstractSearch |
|
79
|
|
|
*/ |
|
80
|
|
|
protected function setItem(array $data): AbstractSearch |
|
81
|
|
|
{ |
|
82
|
|
|
$this->datas[] = [ |
|
83
|
|
|
self::_ID => $data[0], |
|
84
|
|
|
self::_NAME => $data[1], |
|
85
|
|
|
self::_EMAIL => $data[2], |
|
86
|
|
|
self::_PASSWORD => $data[3], |
|
87
|
|
|
self::_STATUS => $data[4], |
|
88
|
|
|
self::_ROLE => $data[5] |
|
89
|
|
|
]; |
|
90
|
|
|
return $this; |
|
91
|
|
|
} |
|
92
|
|
|
|
|
93
|
|
|
/** |
|
94
|
|
|
* create csv file account from config accounts setting |
|
95
|
|
|
* |
|
96
|
|
|
* @param string $filename |
|
97
|
|
|
* @return AbstractSearch |
|
98
|
|
|
*/ |
|
99
|
|
|
protected function createFile(string $filename): AbstractSearch |
|
100
|
|
|
{ |
|
101
|
|
|
if (!file_exists($filename)) { |
|
102
|
|
|
$crypt = new Crypt($this->config); |
|
103
|
|
|
$accounts = $this->config->getSettings(Config::_ACCOUNTS); |
|
104
|
|
|
$accounts = array_map(function ($ac) use ($crypt) { |
|
105
|
|
|
$ac[self::_PASSWORD] = $crypt->encrypt( |
|
106
|
|
|
$ac[self::_PASSWORD], |
|
107
|
|
|
true |
|
108
|
|
|
); |
|
109
|
|
|
return $ac; |
|
110
|
|
|
}, $accounts); |
|
111
|
|
|
$fp = fopen($filename, 'w'); |
|
112
|
|
|
foreach ($accounts as $record) { |
|
113
|
|
|
fputcsv($fp, array_values($record)); |
|
|
|
|
|
|
114
|
|
|
} |
|
115
|
|
|
fclose($fp); |
|
|
|
|
|
|
116
|
|
|
unset($fp, $accounts, $crypt); |
|
117
|
|
|
} |
|
118
|
|
|
return $this; |
|
119
|
|
|
} |
|
120
|
|
|
} |
|
121
|
|
|
|