api.acl.php 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440
  1. <?php
  2. /**
  3. * per-users ACL implementation
  4. */
  5. class ACL {
  6. /**
  7. * Contains ACL database abstraction layer
  8. *
  9. * @var object
  10. */
  11. protected $aclDb = '';
  12. /**
  13. * Contains cameras database abstraction layer
  14. *
  15. * @var object
  16. */
  17. protected $camerasDb = '';
  18. /**
  19. * Contains cameras data as id=>cameraData
  20. *
  21. * @var array
  22. */
  23. protected $allCamerasData = '';
  24. /**
  25. * Contains all available ACLs as aclId=>aclData[id/user/cameraid/channel]
  26. *
  27. * @var array
  28. */
  29. protected $allAcls = array();
  30. /**
  31. * Contains all accessible by all users camerasIds as login=>cameraId=>channel
  32. *
  33. * @var array
  34. */
  35. protected $accessibleCameras = array();
  36. /**
  37. * Contains all accessible by all users channelIds as login=>channelId=>cameraId
  38. *
  39. * @var array
  40. */
  41. protected $accessibleChannels = array();
  42. /**
  43. * System messages helper instance placeholder
  44. *
  45. * @var object
  46. */
  47. protected $messages = '';
  48. /**
  49. * Contains current instance user login
  50. *
  51. * @var string
  52. */
  53. protected $myLogin = '';
  54. /**
  55. * some predefined stuff
  56. */
  57. const TABLE_ACL = 'acl';
  58. const URL_ME = '?module=acl';
  59. const PROUTE_NEWLOGIN = 'newacllogin';
  60. const PROUTE_NEWCAMID = 'newaclcameraid';
  61. const ROUTE_DEL = 'deleteaclid';
  62. public function __construct($loadCameras = false) {
  63. $this->initMessages();
  64. $this->setLogin();
  65. $this->initAclDb();
  66. $this->loadAcls();
  67. if ($loadCameras) {
  68. $this->initCamerasDb();
  69. $this->loadCamerasData();
  70. }
  71. }
  72. /**
  73. * Sets current instance login
  74. *
  75. * @return void
  76. */
  77. protected function setLogin() {
  78. $this->myLogin = whoami();
  79. }
  80. /**
  81. * Inits access control lists database abstraction layer
  82. *
  83. * @return void
  84. */
  85. protected function initAclDb() {
  86. $this->aclDb = new NyanORM(self::TABLE_ACL);
  87. }
  88. /**
  89. * Inits cameras database abstraction layer
  90. *
  91. * @return void
  92. */
  93. protected function initCamerasDb() {
  94. $this->camerasDb = new NyanORM(Cameras::DATA_TABLE);
  95. }
  96. /**
  97. * Loads cameras basic data
  98. *
  99. * @return void
  100. */
  101. protected function loadCamerasData() {
  102. $this->camerasDb->orderBy('id', 'DESC');
  103. $this->camerasDb->selectable(array('id', 'ip', 'active', 'channel', 'comment'));
  104. $this->allCamerasData = $this->camerasDb->getAll('id');
  105. }
  106. /**
  107. * Inits system messages helper
  108. *
  109. * @return void
  110. */
  111. protected function initMessages() {
  112. $this->messages = new UbillingMessageHelper();
  113. }
  114. /**
  115. * Loads all existing ACL data for further usage
  116. *
  117. * @return void
  118. */
  119. protected function loadAcls() {
  120. $this->allAcls = $this->aclDb->getAll('id');
  121. if (!empty($this->allAcls)) {
  122. //some ACL data postprocessing
  123. foreach ($this->allAcls as $io => $each) {
  124. $this->accessibleCameras[$each['user']][$each['cameraid']] = $each['channel'];
  125. $this->accessibleChannels[$each['user']][$each['channel']] = $each['cameraid'];
  126. }
  127. }
  128. }
  129. /**
  130. * Returns all available camera ACLs data as login=>cameraId=>channel
  131. *
  132. * @return array
  133. */
  134. public function getAllCameraAclsData() {
  135. $result = array();
  136. if (!empty($this->allAcls)) {
  137. $result = $this->accessibleCameras;
  138. }
  139. return($result);
  140. }
  141. /**
  142. * Returns all available channel ACLs data as login=>channel=>cameraId
  143. *
  144. * @return array
  145. */
  146. public function getAllChannelAclsData() {
  147. $result = array();
  148. if (!empty($this->allAcls)) {
  149. $result = $this->accessibleChannels;
  150. }
  151. return($result);
  152. }
  153. /**
  154. * Returns all availbale ACLs data as aclId=>aclData[id/user/cameraid/channel]
  155. *
  156. * @return array
  157. */
  158. public function getAllAclsData() {
  159. return($this->allAcls);
  160. }
  161. /**
  162. * Creates new ACL database record
  163. *
  164. * @param string $login
  165. * @param int $cameraId
  166. *
  167. * @return void/string on error
  168. */
  169. public function create($login, $cameraId) {
  170. $result = '';
  171. $loginF = ubRouting::filters($login, 'mres');
  172. $cameraId = ubRouting::filters($cameraId, 'int');
  173. $allCameraAcls = $this->getAllCameraAclsData();
  174. if (!isset($allCameraAcls[$loginF][$cameraId])) {
  175. if (isset($this->allCamerasData[$cameraId])) {
  176. $cameraChannel = $this->allCamerasData[$cameraId]['channel'];
  177. if (file_exists(USERS_PATH . $loginF)) {
  178. $this->aclDb->data('user', $loginF);
  179. $this->aclDb->data('cameraid', $cameraId);
  180. $this->aclDb->data('channel', $cameraChannel);
  181. $this->aclDb->create();
  182. $newId = $this->aclDb->getLastId();
  183. log_register('ACL CREATE [' . $newId . '] CAMERA [' . $cameraId . '] CHANNEL `' . $cameraChannel . '` FOR {' . $login . '}');
  184. } else {
  185. $result .= __('User') . ' {' . $loginF . '} ' . __('not exists');
  186. }
  187. } else {
  188. $result .= __('Camera') . ' [' . $cameraId . '] ' . __('not exists');
  189. }
  190. } else {
  191. $result .= __('ACL') . ' [' . $cameraId . '] ' . __('already exists');
  192. }
  193. return($result);
  194. }
  195. /**
  196. * Creates new ACL database record using camera channelId
  197. *
  198. * @param string $login
  199. * @param int $channelId
  200. *
  201. * @return void/string on error
  202. */
  203. public function assignChannel($login, $channelId) {
  204. $result = '';
  205. if (!empty($this->allCamerasData)) {
  206. $cameraId = 0;
  207. foreach ($this->allCamerasData as $io => $each) {
  208. if ($each['channel'] == $channelId) {
  209. $cameraId = $each['id'];
  210. }
  211. }
  212. if ($cameraId) {
  213. $result = $this->create($login, $cameraId);
  214. } else {
  215. $result .= __('Channel') . ' ' . __('not exists');
  216. }
  217. }
  218. return($result);
  219. }
  220. /**
  221. * Deletes existing ACL from database
  222. *
  223. * @param int $aclId
  224. *
  225. * @return void/string on error
  226. */
  227. public function delete($aclId) {
  228. $result = '';
  229. $aclId = ubRouting::filters($aclId, 'int');
  230. if (isset($this->allAcls[$aclId])) {
  231. $aclData = $this->allAcls[$aclId];
  232. $this->aclDb->where('id', '=', $aclId);
  233. $this->aclDb->delete();
  234. log_register('ACL DELETE [' . $aclId . '] CAMERA [' . $aclData['cameraid'] . '] CHANNEL `' . $aclData['channel'] . '` FOR {' . $aclData['user'] . '}');
  235. } else {
  236. $result .= __('ACL') . ' [' . $aclId . '] ' . __('not exists');
  237. }
  238. return($result);
  239. }
  240. /**
  241. * Flushes all available ACLs for some user
  242. *
  243. * @param string $login
  244. *
  245. * @return void
  246. */
  247. public function flushUser($login) {
  248. $loginF = ubRouting::filters($login, 'mres');
  249. $this->aclDb->where('user', '=', $loginF);
  250. $this->aclDb->delete();
  251. log_register('ACL FLUSH USER {' . $login . '}');
  252. }
  253. /**
  254. * Flushes all available ACLs for some camera
  255. *
  256. * @param string $cameraId
  257. *
  258. * @return void
  259. */
  260. public function flushCamera($cameraId) {
  261. $cameraId = ubRouting::filters($cameraId, 'mres');
  262. $this->aclDb->where('cameraid', '=', $cameraId);
  263. $this->aclDb->delete();
  264. log_register('ACL FLUSH CAMERA [' . $cameraId . ']');
  265. }
  266. /**
  267. * Returns availablilty of cameras accessible by user
  268. *
  269. * @return int
  270. */
  271. public function haveCamsAssigned() {
  272. $result = false;
  273. if (!cfr('ROOT') AND !cfr('OPERATOR')) {
  274. if (!empty($this->myLogin)) {
  275. if (!empty($this->allAcls)) {
  276. if (isset($this->accessibleCameras[$this->myLogin])) {
  277. $result = true;
  278. }
  279. }
  280. }
  281. } else {
  282. $result = true;
  283. }
  284. return($result);
  285. }
  286. /**
  287. * Checks is some camera allowed for current user?
  288. *
  289. * @param int $cameraId
  290. *
  291. * @return bool
  292. */
  293. public function isMyCamera($cameraId) {
  294. $result = false;
  295. if (!cfr('ROOT') AND !cfr('OPERATOR')) {
  296. if (!empty($this->myLogin)) {
  297. if (!empty($this->allAcls)) {
  298. if (isset($this->accessibleCameras[$this->myLogin])) {
  299. if (isset($this->accessibleCameras[$this->myLogin][$cameraId])) {
  300. $result = true;
  301. }
  302. }
  303. }
  304. }
  305. } else {
  306. //all cameras is accessible by ROOT and OPERATOR users
  307. $result = true;
  308. }
  309. return($result);
  310. }
  311. /**
  312. * Checks is some camera channel allowed for current user?
  313. *
  314. * @param string $channelId
  315. *
  316. * @return bool
  317. */
  318. public function isMyChannel($channelId) {
  319. $result = false;
  320. if (!cfr('ROOT') AND !cfr('OPERATOR')) {
  321. if (!empty($this->myLogin)) {
  322. if (!empty($this->allAcls)) {
  323. if (isset($this->accessibleChannels[$this->myLogin])) {
  324. if (isset($this->accessibleChannels[$this->myLogin][$channelId])) {
  325. $result = true;
  326. }
  327. }
  328. }
  329. }
  330. } else {
  331. //all cameras is accessible by ROOT and OPERATOR users
  332. $result = true;
  333. }
  334. return($result);
  335. }
  336. /**
  337. * Renders existing ACLs list
  338. *
  339. * @return string
  340. */
  341. public function renderAclList() {
  342. $result = '';
  343. if (!empty($this->allAcls)) {
  344. $cells = wf_TableCell(__('User'));
  345. $cells .= wf_TableCell(__('Camera'));
  346. $cells .= wf_TableCell(__('Actions'));
  347. $rows = wf_TableRow($cells, 'row1');
  348. foreach ($this->allAcls as $io => $each) {
  349. $cameraLabel = '';
  350. if (isset($this->allCamerasData[$each['cameraid']])) {
  351. $cameraData = $this->allCamerasData[$each['cameraid']];
  352. $cameraLabel = $cameraData['ip'] . ' - ' . $cameraData['comment'];
  353. } else {
  354. $cameraLabel = '[' . $each['cameraid'] . '] ' . __('Lost');
  355. }
  356. $cells = wf_TableCell($each['user']);
  357. $cells .= wf_TableCell($cameraLabel, '', '', 'sorttable_customkey="' . $each['cameraid'] . '"');
  358. $delUrl = self::URL_ME . '&' . self::ROUTE_DEL . '=' . $each['id'];
  359. $cancelUrl = self::URL_ME;
  360. $actLinks = wf_ConfirmDialog($delUrl, web_delete_icon(), $this->messages->getDeleteAlert(), '', $cancelUrl, __('Delete') . '?');
  361. $cells .= wf_TableCell($actLinks);
  362. $rows .= wf_TableRow($cells, 'row5');
  363. }
  364. $result .= wf_TableBody($rows, '100%', 0, 'sortable resp-table');
  365. } else {
  366. $result .= $this->messages->getStyledMessage(__('Nothing to show'), 'info');
  367. }
  368. return($result);
  369. }
  370. /**
  371. * Renders new ACL creation form
  372. *
  373. * @return string
  374. */
  375. public function renderCreateForm() {
  376. $result = '';
  377. $allUsers = rcms_scandir(USERS_PATH);
  378. $usersParams = array('' => '-');
  379. $camerasParams = array('' => '-');
  380. if (!empty($allUsers)) {
  381. foreach ($allUsers as $io => $eachUser) {
  382. $usersParams[$eachUser] = $eachUser;
  383. }
  384. } else {
  385. $result .= $this->messages->getStyledMessage(__('Available users') . ': ' . __('not exists'), 'error');
  386. }
  387. if (!empty($this->allCamerasData)) {
  388. foreach ($this->allCamerasData as $io => $eachCameraData) {
  389. $camerasParams[$eachCameraData['id']] = $eachCameraData['ip'] . ' - ' . $eachCameraData['comment'];
  390. }
  391. } else {
  392. $result .= $this->messages->getStyledMessage(__('Cameras') . ': ' . __('not exists'), 'error');
  393. }
  394. //preprocessed data not empty?
  395. if ($usersParams AND $camerasParams) {
  396. $inputs = wf_SelectorSearchable(self::PROUTE_NEWLOGIN, $usersParams, __('User'), '', false) . ' ';
  397. $inputs .= wf_SelectorSearchable(self::PROUTE_NEWCAMID, $camerasParams, __('Camera'), '', false) . ' ';
  398. $inputs .= wf_Submit(__('Create'));
  399. $result .= wf_Form('', 'POST', $inputs, 'glamour');
  400. }
  401. return($result);
  402. }
  403. }