123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228 |
- <?php
- namespace App\Controller;
-
- use App\Utility\EprosumeModel;
- use App\Utility\WalletEprosumeModel;
-
- use Cake\Core\Configure;
- use Cake\ORM\TableRegistry;
- use Cake\Http\Exception\ForbiddenException;
- use Cake\Http\Exception\NotFoundException;
- use Cake\View\Exception\MissingTemplateException;
-
-
- class MainController extends AppController
- {
- protected $EprosumeModel = null;
- protected $WalletEprosumeModel = null;
-
- public function initialize()
- {
- parent::initialize();
- $this->EprosumeModel = new EprosumeModel();
- $this->WalletEprosumeModel = new WalletEprosumeModel();
- }
-
- public function history($filter=[])
- {
- $user = $this->Auth->user();
- $this->set(['blocks' => []]);
- $this->set(['prosumers' => $this->WalletEprosumeModel->getProsumers(false, $user["meter_id"])]);
- }
-
- public function historyDaily()
- {
- $this->history(
- ['daily'=>true]
- );
- }
-
- public function meter()
- {
- $user = $this->Auth->user();
- $role = $user["role"] ?? null;
- if($role && $role != "admin") {
- $_ = $this->EprosumeModel->meterList($user["meter_id"]);
- return $this->redirect( (($_["name"] ?? null) ? '/web/balance/'.$_["name"] : '/' ) );
- }
- $this->set(['meterList' => $this->EprosumeModel->meterList()]);
- $this->set(['prices' => $this->EprosumeModel->actualPrice([])]);
- }
-
- public function priceHistory()
- {
- $this->history();
- }
-
- public function payment($meter_id = null)
- {
- $user = $this->Auth->user();
- $this->set(['blocks' => []]);
- $this->set(['meter_id' => $meter_id]);
- $this->set(['prosumers' => $this->WalletEprosumeModel->getProsumers(false, $user["meter_id"])]);
- }
-
- public function historyMeter($meter_id)
- {
- $this->history(['wallet' => $meter_id]);
- $this->set(['meter_id' => $meter_id]);
- }
-
- public function blockID($block_id) {
- $this->set(['block' => $this->EprosumeModel->blocksList(['id' => $block_id])->current()]);
- }
-
- public function meterID($meter_id)
- {
- $user = $this->Auth->user();
- $role = $user["role"] ?? null;
- if($role && $role != "admin") {
- $_ = $this->EprosumeModel->meterList($user["meter_id"]);
- if(($_["name"] ?? null) != $meter_id) {
- return $this->redirect('/');
- }
- }
- $this->set(['balance' => $this->EprosumeModel->balanceMeter($meter_id)]);
- $this->set(['prices' => $this->EprosumeModel->actualPrice([])]);
- $this->set(['meter_id' => $meter_id]);
- $this->set(['utility_account' => $this->EprosumeModel->getUtilityAccount()]);
- }
-
- public function display(...$path)
- {
- $count = count($path);
- if (!$count) {
- return $this->redirect('/');
- }
- if (in_array('..', $path, true) || in_array('.', $path, true)) {
- throw new ForbiddenException();
- }
- $page = $subpage = null;
-
- if (!empty($path[0])) {
- $page = $path[0];
- }
- if (!empty($path[1])) {
- $subpage = $path[1];
- }
- $this->set(compact('page', 'subpage'));
-
- try {
- $this->render(implode('/', $path));
- } catch (MissingTemplateException $exception) {
- if (Configure::read('debug')) {
- throw $exception;
- }
- throw new NotFoundException();
- }
- }
-
- public function setPrice()
- {
- $this->set(
- [
- 'prices' => (function() {
- $prices = $this->EprosumeModel->actualPrice([]);
- unset($prices[$this->EprosumeModel->getUtilityAccount()]);
- ksort($prices);
- return $prices;
- })()
- ]
- );
- }
-
- public function transfer()
- {
- $user = $this->Auth->user();
- $role = $user['role'] ?? null;
- $user_meter_id = $user["meter_id"] ?? null;
- if(!$role || !$user_meter_id) {
- return $this->redirect('/');
- }
- $this->set(
- [
- 'assets' => $this->EprosumeModel->getAssetsType(),
- 'prosumers' => (function() use($user) {
- $_ = $this->WalletEprosumeModel->getProsumers(false, $user["meter_id"]);
- $prosumers = array_combine(
- array_values($_),
- $_
- );
- ksort($prosumers);
- return $prosumers;
- })(),
- 'prosumersHistory' => $this->WalletEprosumeModel->getProsumers(true)
- ]
- );
- }
-
- public function charts()
- {
- $this->set(
- [
- 'prosumers' => $this->WalletEprosumeModel->getProsumers(1)
- ]
- );
- }
-
- public function isAuthorized($user)
- {
- if (
- (
- $this->request->getParam('action') === 'setPrice'
- )
- ) {
- return $user["role"] === "admin";
- }
- return parent::isAuthorized($user);
- }
-
- public function profile()
- {
- $user = $this->Auth->user();
- $outcome = null;
-
- if ($this->request->is('post')) {
- if($this->Auth->identify()) {
- $data = $this->request->getData();
- if(preg_match('/^(?=[^\d]*\d)(?=[A-Z\d ]*[^A-Z\d ]).{8,}$/i', ($data['new_password'] ?? ""))) {
- if($data['new_password'] === $data['retype_password']) {
- $Users = TableRegistry::getTableLocator()->get('Users');
- $_user = $Users->patchEntity(
- $Users
- ->find()
- ->where(['id' => $user["id"]])
- ->first()
- ,
- [
- 'password' => $data['new_password'],
- ],
- [
- 'validate' => false
- ]
- );
- if($Users->save($_user)) {
- $outcome = true;
- $this->Flash->success(__('The new password has been successfully set.'));
- } else {
- $outcome = false;
- $this->Flash->error(__('Sorry, an error occured trying to update your password.'));
- }
- } else {
- $this->Flash->error(__('The new password has not been properly retyped.'));
- }
- } else {
- $this->Flash->error(__('The new password must be at least of 8 characters and with a capital letter and a special character.'));
- }
- } else {
- $this->Flash->error(__('Wrong current password.'));
- }
- }
- $this->set(
- [
- 'user' => $user,
- 'outcome' => $outcome
- ]
- );
- }
- }
|