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: * @property Settings $oModuleSettings
20: *
21: * @package Modules
22: */
23: class Module extends \Aurora\System\Module\AbstractModule
24: {
25: public $oManager = null;
26:
27: /**
28: * @return Module
29: */
30: public static function getInstance()
31: {
32: return parent::getInstance();
33: }
34:
35: /**
36: * @return Module
37: */
38: public static function Decorator()
39: {
40: return parent::Decorator();
41: }
42:
43: /**
44: * @return Settings
45: */
46: public function getModuleSettings()
47: {
48: return $this->oModuleSettings;
49: }
50:
51: /***** private functions *****/
52: /**
53: * Initializes module.
54: *
55: * @ignore
56: */
57: public function init()
58: {
59: $this->oManager = new Manager($this);
60:
61: $this->aDeniedMethodsByWebApi = [
62: 'CreateMin',
63: 'DeleteMinByHash',
64: 'DeleteMinByID',
65: 'GetMinByHash',
66: 'GetMinByID',
67: 'UpdateMinByHash',
68: 'UpdateMinByID',
69: 'DeleteExpiredHashes',
70: 'generateHashId'
71: ];
72: $this->subscribeEvent('Core::DeleteUser::after', array($this, 'onAfterDeleteUser'));
73: }
74:
75: /***** private functions *****/
76:
77: /***** public functions *****/
78: public function onAfterDeleteUser($aArgs, &$mResult)
79: {
80: if ($mResult) {
81: MinHash::where('UserId', $aArgs['UserId'])->delete();
82: }
83: }
84:
85: public static function generateHashId($aData)
86: {
87: return \md5(\implode('|', $aData));
88: }
89:
90: /***** public functions might be called with web API *****/
91: /**
92: * Crates min hash.
93: *
94: * @param string $HashId Hash identifier.
95: * @param array $Parameters Hash parameters.
96: * @param int $UserId User identifier.
97: * @param int $ExpireDate
98: * @return string|boolean
99: */
100: public function CreateMin($HashId, $Parameters, $UserId = null, $ExpireDate = null)
101: {
102: \Aurora\System\Api::checkUserRoleIsAtLeast(\Aurora\System\Enums\UserRole::NormalUser);
103:
104: return $this->oManager->createMin($HashId, $Parameters, $UserId, $ExpireDate);
105: }
106:
107: /**
108: * Returns parameters object by min hash.
109: *
110: * @param string $sHash Min hash.
111: * @return array|bool
112: */
113: public function GetMinByHash($sHash)
114: {
115: \Aurora\System\Api::checkUserRoleIsAtLeast(\Aurora\System\Enums\UserRole::Anonymous);
116:
117: return $this->oManager->getMinByHash($sHash);
118: }
119:
120: /**
121: * Returns parameters object by min hash identifier.
122: *
123: * @param string $Id
124: * @return array|bool
125: */
126: public function GetMinByID($Id)
127: {
128: \Aurora\System\Api::checkUserRoleIsAtLeast(\Aurora\System\Enums\UserRole::Anonymous);
129:
130: return $this->oManager->getMinByID($Id);
131: }
132:
133: /**
134: * @deprecated since version 9.7.3
135: *
136: * @param int $UserId
137: * @return array|bool
138: */
139: public function GetMinListByUserId($UserId)
140: {
141: \Aurora\System\Api::checkUserRoleIsAtLeast(\Aurora\System\Enums\UserRole::Anonymous);
142:
143: return $this->oManager->getMinListByUserId($UserId);
144: }
145:
146: /**
147: * Updates min hash by min hash identifier.
148: *
149: * @param string $Id Hash identifier.
150: * @param array $Data Hash parameters.
151: * @param string $NewId New hash identifier.
152: * @return boolean
153: */
154: public function UpdateMinByID($Id, $Data, $NewId = null)
155: {
156: \Aurora\System\Api::checkUserRoleIsAtLeast(\Aurora\System\Enums\UserRole::NormalUser);
157:
158: return $this->oManager->updateMinByID($Id, $Data, $NewId);
159: }
160:
161: /**
162: * Updates min hash by min hash.
163: *
164: * @param string $Hash Min hash.
165: * @param array $Data Hash parameters.
166: * @param string $NewHash New min hash.
167: * @return boolean
168: */
169: public function UpdateMinByHash($Hash, $Data, $NewHash = null)
170: {
171: \Aurora\System\Api::checkUserRoleIsAtLeast(\Aurora\System\Enums\UserRole::Anonymous);
172:
173: return $this->oManager->updateMinByHash($Hash, $Data, $NewHash);
174: }
175:
176: /**
177: * Deletes min hash by min hash identifier.
178: *
179: * @param string $Id
180: * @return boolean
181: */
182: public function DeleteMinByID($Id)
183: {
184: \Aurora\System\Api::checkUserRoleIsAtLeast(\Aurora\System\Enums\UserRole::NormalUser);
185:
186: return $this->oManager->deleteMinByID($Id);
187: }
188:
189: public function DeleteMinByHash($Hash)
190: {
191: \Aurora\System\Api::checkUserRoleIsAtLeast(\Aurora\System\Enums\UserRole::Anonymous);
192:
193: return $this->oManager->deleteMinByHash($Hash);
194: }
195:
196: public function DeleteExpiredHashes($Time)
197: {
198: \Aurora\System\Api::checkUserRoleIsAtLeast(\Aurora\System\Enums\UserRole::Anonymous);
199: Models\MinHash::whereNotNull('ExpireDate')->where('ExpireDate', '<=', $Time)->delete();
200: }
201: /***** public functions might be called with web API *****/
202: }
203: