Web Interfaces of PROSUME

MainController.php 7.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228
  1. <?php
  2. namespace App\Controller;
  3. use App\Utility\EprosumeModel;
  4. use App\Utility\WalletEprosumeModel;
  5. use Cake\Core\Configure;
  6. use Cake\ORM\TableRegistry;
  7. use Cake\Http\Exception\ForbiddenException;
  8. use Cake\Http\Exception\NotFoundException;
  9. use Cake\View\Exception\MissingTemplateException;
  10. class MainController extends AppController
  11. {
  12. protected $EprosumeModel = null;
  13. protected $WalletEprosumeModel = null;
  14. public function initialize()
  15. {
  16. parent::initialize();
  17. $this->EprosumeModel = new EprosumeModel();
  18. $this->WalletEprosumeModel = new WalletEprosumeModel();
  19. }
  20. public function history($filter=[])
  21. {
  22. $user = $this->Auth->user();
  23. $this->set(['blocks' => []]);
  24. $this->set(['prosumers' => $this->WalletEprosumeModel->getProsumers(false, $user["meter_id"])]);
  25. }
  26. public function historyDaily()
  27. {
  28. $this->history(
  29. ['daily'=>true]
  30. );
  31. }
  32. public function meter()
  33. {
  34. $user = $this->Auth->user();
  35. $role = $user["role"] ?? null;
  36. if($role && $role != "admin") {
  37. $_ = $this->EprosumeModel->meterList($user["meter_id"]);
  38. return $this->redirect( (($_["name"] ?? null) ? '/web/balance/'.$_["name"] : '/' ) );
  39. }
  40. $this->set(['meterList' => $this->EprosumeModel->meterList()]);
  41. $this->set(['prices' => $this->EprosumeModel->actualPrice([])]);
  42. }
  43. public function priceHistory()
  44. {
  45. $this->history();
  46. }
  47. public function payment($meter_id = null)
  48. {
  49. $user = $this->Auth->user();
  50. $this->set(['blocks' => []]);
  51. $this->set(['meter_id' => $meter_id]);
  52. $this->set(['prosumers' => $this->WalletEprosumeModel->getProsumers(false, $user["meter_id"])]);
  53. }
  54. public function historyMeter($meter_id)
  55. {
  56. $this->history(['wallet' => $meter_id]);
  57. $this->set(['meter_id' => $meter_id]);
  58. }
  59. public function blockID($block_id) {
  60. $this->set(['block' => $this->EprosumeModel->blocksList(['id' => $block_id])->current()]);
  61. }
  62. public function meterID($meter_id)
  63. {
  64. $user = $this->Auth->user();
  65. $role = $user["role"] ?? null;
  66. if($role && $role != "admin") {
  67. $_ = $this->EprosumeModel->meterList($user["meter_id"]);
  68. if(($_["name"] ?? null) != $meter_id) {
  69. return $this->redirect('/');
  70. }
  71. }
  72. $this->set(['balance' => $this->EprosumeModel->balanceMeter($meter_id)]);
  73. $this->set(['prices' => $this->EprosumeModel->actualPrice([])]);
  74. $this->set(['meter_id' => $meter_id]);
  75. $this->set(['utility_account' => $this->EprosumeModel->getUtilityAccount()]);
  76. }
  77. public function display(...$path)
  78. {
  79. $count = count($path);
  80. if (!$count) {
  81. return $this->redirect('/');
  82. }
  83. if (in_array('..', $path, true) || in_array('.', $path, true)) {
  84. throw new ForbiddenException();
  85. }
  86. $page = $subpage = null;
  87. if (!empty($path[0])) {
  88. $page = $path[0];
  89. }
  90. if (!empty($path[1])) {
  91. $subpage = $path[1];
  92. }
  93. $this->set(compact('page', 'subpage'));
  94. try {
  95. $this->render(implode('/', $path));
  96. } catch (MissingTemplateException $exception) {
  97. if (Configure::read('debug')) {
  98. throw $exception;
  99. }
  100. throw new NotFoundException();
  101. }
  102. }
  103. public function setPrice()
  104. {
  105. $this->set(
  106. [
  107. 'prices' => (function() {
  108. $prices = $this->EprosumeModel->actualPrice([]);
  109. unset($prices[$this->EprosumeModel->getUtilityAccount()]);
  110. ksort($prices);
  111. return $prices;
  112. })()
  113. ]
  114. );
  115. }
  116. public function transfer()
  117. {
  118. $user = $this->Auth->user();
  119. $role = $user['role'] ?? null;
  120. $user_meter_id = $user["meter_id"] ?? null;
  121. if(!$role || !$user_meter_id) {
  122. return $this->redirect('/');
  123. }
  124. $this->set(
  125. [
  126. 'assets' => $this->EprosumeModel->getAssetsType(),
  127. 'prosumers' => (function() use($user) {
  128. $_ = $this->WalletEprosumeModel->getProsumers(false, $user["meter_id"]);
  129. $prosumers = array_combine(
  130. array_values($_),
  131. $_
  132. );
  133. ksort($prosumers);
  134. return $prosumers;
  135. })(),
  136. 'prosumersHistory' => $this->WalletEprosumeModel->getProsumers(true)
  137. ]
  138. );
  139. }
  140. public function charts()
  141. {
  142. $this->set(
  143. [
  144. 'prosumers' => $this->WalletEprosumeModel->getProsumers(1)
  145. ]
  146. );
  147. }
  148. public function isAuthorized($user)
  149. {
  150. if (
  151. (
  152. $this->request->getParam('action') === 'setPrice'
  153. )
  154. ) {
  155. return $user["role"] === "admin";
  156. }
  157. return parent::isAuthorized($user);
  158. }
  159. public function profile()
  160. {
  161. $user = $this->Auth->user();
  162. $outcome = null;
  163. if ($this->request->is('post')) {
  164. if($this->Auth->identify()) {
  165. $data = $this->request->getData();
  166. if(preg_match('/^(?=[^\d]*\d)(?=[A-Z\d ]*[^A-Z\d ]).{8,}$/i', ($data['new_password'] ?? ""))) {
  167. if($data['new_password'] === $data['retype_password']) {
  168. $Users = TableRegistry::getTableLocator()->get('Users');
  169. $_user = $Users->patchEntity(
  170. $Users
  171. ->find()
  172. ->where(['id' => $user["id"]])
  173. ->first()
  174. ,
  175. [
  176. 'password' => $data['new_password'],
  177. ],
  178. [
  179. 'validate' => false
  180. ]
  181. );
  182. if($Users->save($_user)) {
  183. $outcome = true;
  184. $this->Flash->success(__('The new password has been successfully set.'));
  185. } else {
  186. $outcome = false;
  187. $this->Flash->error(__('Sorry, an error occured trying to update your password.'));
  188. }
  189. } else {
  190. $this->Flash->error(__('The new password has not been properly retyped.'));
  191. }
  192. } else {
  193. $this->Flash->error(__('The new password must be at least of 8 characters and with a capital letter and a special character.'));
  194. }
  195. } else {
  196. $this->Flash->error(__('Wrong current password.'));
  197. }
  198. }
  199. $this->set(
  200. [
  201. 'user' => $user,
  202. 'outcome' => $outcome
  203. ]
  204. );
  205. }
  206. }