1: <?php
2: /**
3: * This code is licensed under AGPLv3 license or Afterlogic Software License
4: * if commercial version of the product was purchased.
5: * For full statements of the licenses see LICENSE-AFTERLOGIC and LICENSE-AGPL3 files.
6: */
7:
8: namespace Aurora\Modules\Min;
9:
10: use Aurora\Modules\Min\Models\MinHash;
11:
12: /**
13: * System module provides hash-based object storage.
14: *
15: * @license https://www.gnu.org/licenses/agpl-3.0.html AGPL-3.0
16: * @license https://afterlogic.com/products/common-licensing Afterlogic Software License
17: * @copyright Copyright (c) 2023, Afterlogic Corp.
18: *
19: * @package Modules
20: */
21: class Module extends \Aurora\System\Module\AbstractModule
22: {
23: public $oManager = null;
24:
25: /**
26: *
27: * @return Module
28: */
29: public static function getInstance()
30: {
31: return \Aurora\System\Api::GetModule(self::GetName());
32: }
33:
34: /***** private functions *****/
35: /**
36: * Initializes module.
37: *
38: * @ignore
39: */
40: public function init()
41: {
42: $this->oManager = new Manager($this);
43:
44: $this->aDeniedMethodsByWebApi = [
45: 'CreateMin',
46: 'DeleteMinByHash',
47: 'DeleteMinByID',
48: 'GetMinByHash',
49: 'GetMinByID',
50: 'UpdateMinByHash',
51: 'UpdateMinByID'
52: ];
53: $this->subscribeEvent('Core::DeleteUser::after', array($this, 'onAfterDeleteUser'));
54: }
55:
56: /***** private functions *****/
57:
58: /***** public functions *****/
59: public function onAfterDeleteUser($aArgs, &$mResult)
60: {
61: if ($mResult) {
62: MinHash::where('UserId', $aArgs['UserId'])->delete();
63: }
64: }
65:
66: public static function generateHashId($aData)
67: {
68: return \md5(\implode('|', $aData));
69: }
70:
71: /***** public functions might be called with web API *****/
72: /**
73: * Crates min hash.
74: *
75: * @param string $HashId Hash identifier.
76: * @param array $Parameters Hash parameters.
77: * @param int $UserId User identifier.
78: * @param int $ExpireDate
79: * @return string|boolean
80: */
81: public function CreateMin($HashId, $Parameters, $UserId = null, $ExpireDate = null)
82: {
83: \Aurora\System\Api::checkUserRoleIsAtLeast(\Aurora\System\Enums\UserRole::NormalUser);
84:
85: return $this->oManager->createMin($HashId, $Parameters, $UserId, $ExpireDate);
86: }
87:
88: /**
89: * Returns parameters object by min hash.
90: *
91: * @param string $sHash Min hash.
92: * @return array|bool
93: */
94: public function GetMinByHash($sHash)
95: {
96: \Aurora\System\Api::checkUserRoleIsAtLeast(\Aurora\System\Enums\UserRole::Anonymous);
97:
98: return $this->oManager->getMinByHash($sHash);
99: }
100:
101: /**
102: * Returns parameters object by min hash identifier.
103: *
104: * @param string $Id
105: * @return array|bool
106: */
107: public function GetMinByID($Id)
108: {
109: \Aurora\System\Api::checkUserRoleIsAtLeast(\Aurora\System\Enums\UserRole::Anonymous);
110:
111: return $this->oManager->getMinByID($Id);
112: }
113:
114: /**
115: * Returns parameters object by min hash identifier.
116: *
117: * @param int $Id
118: * @return array|bool
119: */
120: public function GetMinListByUserId($UserId)
121: {
122: \Aurora\System\Api::checkUserRoleIsAtLeast(\Aurora\System\Enums\UserRole::Anonymous);
123:
124: return $this->oManager->getMinListByUserId($UserId);
125: }
126:
127: /**
128: * Updates min hash by min hash identifier.
129: *
130: * @param string $Id Hash identifier.
131: * @param array $Data Hash parameters.
132: * @param string $NewId New hash identifier.
133: * @return boolean
134: */
135: public function UpdateMinByID($Id, $Data, $NewId = null)
136: {
137: \Aurora\System\Api::checkUserRoleIsAtLeast(\Aurora\System\Enums\UserRole::NormalUser);
138:
139: return $this->oManager->updateMinByID($Id, $Data, $NewId);
140: }
141:
142: /**
143: * Updates min hash by min hash.
144: *
145: * @param string $Hash Min hash.
146: * @param array $Data Hash parameters.
147: * @param string $NewHash New min hash.
148: * @return boolean
149: */
150: public function UpdateMinByHash($Hash, $Data, $NewHash = null)
151: {
152: \Aurora\System\Api::checkUserRoleIsAtLeast(\Aurora\System\Enums\UserRole::Anonymous);
153:
154: return $this->oManager->updateMinByHash($Hash, $Data, $NewHash);
155: }
156:
157: /**
158: * Deletes min hash by min hash identifier.
159: *
160: * @param string $Id
161: * @return boolean
162: */
163: public function DeleteMinByID($Id)
164: {
165: \Aurora\System\Api::checkUserRoleIsAtLeast(\Aurora\System\Enums\UserRole::NormalUser);
166:
167: return $this->oManager->deleteMinByID($Id);
168: }
169:
170: public function DeleteMinByHash($Hash)
171: {
172: \Aurora\System\Api::checkUserRoleIsAtLeast(\Aurora\System\Enums\UserRole::Anonymous);
173:
174: return $this->oManager->deleteMinByHash($Hash);
175: }
176:
177: public function DeleteExpiredHashes($Time)
178: {
179: \Aurora\System\Api::checkUserRoleIsAtLeast(\Aurora\System\Enums\UserRole::Anonymous);
180:
181: Models\MinHash::whereNotNull('ExpireDate')->where('ExpireDate', '<=', $Time)->delete();
182: }
183: /***** public functions might be called with web API *****/
184: }
185: