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\CorporateFiles;
9:
10: /**
11: * Main Files module. It provides PHP and Web APIs for managing files.
12: *
13: * @license https://www.gnu.org/licenses/agpl-3.0.html AGPL-3.0
14: * @license https://afterlogic.com/products/common-licensing Afterlogic Software License
15: * @copyright Copyright (c) 2023, Afterlogic Corp.
16: *
17: * @property Settings $oModuleSettings
18: *
19: * @package Modules
20: */
21: class Module extends \Aurora\Modules\PersonalFiles\Module
22: {
23: protected static $sStorageType = 'corporate';
24: protected static $iStorageOrder = 20;
25:
26: public function init()
27: {
28: parent::init();
29:
30: $this->subscribeEvent('Files::GetQuota::after', array($this, 'onAfterGetQuota'));
31: }
32:
33: /**
34: * @return Module
35: */
36: public static function getInstance()
37: {
38: return parent::getInstance();
39: }
40:
41: /**
42: * @return Module
43: */
44: public static function Decorator()
45: {
46: return parent::Decorator();
47: }
48:
49: /**
50: * @return Settings
51: */
52: public function getModuleSettings()
53: {
54: return $this->oModuleSettings;
55: }
56:
57: /**
58: * Obtains list of module settings.
59: *
60: * @return array
61: */
62: public function GetSettings()
63: {
64: \Aurora\System\Api::checkUserRoleIsAtLeast(\Aurora\System\Enums\UserRole::Anonymous);
65:
66: return array(
67: 'SpaceLimitMb' => $this->oModuleSettings->SpaceLimitMb,
68: );
69: }
70:
71: /**
72: * Updates module's settings - saves them to config.json file.
73: *
74: * @param int $SpaceLimitMb Space limit setting in Mb.
75: * @return bool
76: */
77: public function UpdateSettings($SpaceLimitMb)
78: {
79: \Aurora\System\Api::checkUserRoleIsAtLeast(\Aurora\System\Enums\UserRole::TenantAdmin);
80:
81: $this->setConfig('SpaceLimitMb', $SpaceLimitMb);
82: return (bool) $this->saveModuleConfig();
83: }
84:
85: public function UpdateUsedSpace()
86: {
87: $iResult = 0;
88:
89: \Aurora\System\Api::checkUserRoleIsAtLeast(\Aurora\System\Enums\UserRole::NormalUser);
90: $oUser = \Aurora\System\Api::getAuthenticatedUser();
91:
92: if ($oUser) {
93: $oTenant = \Aurora\Modules\Core\Module::Decorator()->GetTenantWithoutRoleCheck($oUser->IdTenant);
94:
95: if ($oTenant) {
96: $iResult = $this->getManager()->getUserSpaceUsed($oUser->PublicId, [\Aurora\System\Enums\FileStorageType::Corporate]);
97: $oTenant->setExtendedProp(self::GetName() . '::UsedSpace', $iResult);
98: $oTenant->save();
99: }
100: }
101:
102: return $iResult;
103: }
104:
105:
106: public function onAfterGetQuota($aArgs, &$mResult)
107: {
108: if ($this->checkStorageType($aArgs['Type'])) {
109: $iSize = 0;
110:
111: $oUser = \Aurora\Modules\Core\Module::Decorator()->GetUserWithoutRoleCheck((int)$aArgs['UserId']);
112:
113: if ($oUser) {
114: $oTenant = \Aurora\Modules\Core\Module::Decorator()->GetTenantWithoutRoleCheck($oUser->IdTenant);
115:
116: if ($oTenant) {
117: $iSize = null !== $oTenant->getExtendedProp(self::GetName() . '::UsedSpace') ? (int) $oTenant->getExtendedProp(self::GetName() . '::UsedSpace') : 0;
118: }
119: }
120:
121: $mResult = array(
122: 'Used' => (int) $iSize,
123: 'Limit' => $this->oModuleSettings->SpaceLimitMb * 1024 * 1024
124: );
125: }
126: }
127: }
128: