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\OAuthIntegratorWebclient;
9:
10: use Aurora\Modules\OAuthIntegratorWebclient\Models\OauthAccount;
11:
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 Module $oModule
18: */
19: class Manager extends \Aurora\System\Managers\AbstractManager
20: {
21: public function __construct(\Aurora\System\Module\AbstractModule $oModule = null)
22: {
23: parent::__construct($oModule);
24: }
25:
26: /**
27: * @param int $iUserId
28: * @param string $sType
29: *
30: * @return \Aurora\Modules\OAuthIntegratorWebclient\Models\OauthAccount
31: */
32: public function getAccount($iUserId, $sType, $sEmail = '')
33: {
34: $mResult = false;
35:
36: $oQuery = OauthAccount::where('IdUser', $iUserId)->where('Type', $sType);
37: if (!empty($sEmail)) {
38: $oQuery = $oQuery->where('Email', $sEmail);
39: }
40: try {
41: $mResult = $oQuery->first();
42: } catch (\Aurora\System\Exceptions\BaseException $oException) {
43: $mResult = false;
44: $this->setLastException($oException);
45: }
46: return $mResult;
47: }
48:
49: /**
50: * @param string $sIdSocial
51: * @param string $sType
52: *
53: * @return OauthAccount|bool
54: */
55: public function getAccountById($sIdSocial, $sType)
56: {
57: $mResult = false;
58: try {
59: $mResult = OauthAccount::where('IdSocial', $sIdSocial)->where('Type', $sType)->first();
60: } catch (\Aurora\System\Exceptions\BaseException $oException) {
61: $mResult = false;
62: $this->setLastException($oException);
63: }
64: return $mResult;
65: }
66:
67: /**
68: * @param int $iIdUser
69: *
70: * @return array
71: */
72: public function getAccounts($iIdUser)
73: {
74: $aResult = false;
75: try {
76: $aResult = OauthAccount::where('IdUser', $iIdUser)->get();
77: } catch (\Aurora\System\Exceptions\BaseException $oException) {
78: $aResult = false;
79: $this->setLastException($oException);
80: }
81: return $aResult;
82: }
83:
84: /**
85: * @param \Aurora\Modules\OAuthIntegratorWebclient\Models\OauthAccount &$oAccount
86: *
87: * @return bool
88: */
89: public function createAccount(OauthAccount &$oAccount)
90: {
91: $bResult = false;
92: try {
93: if ($oAccount->validate()) {
94: if (!$this->isExists($oAccount)) {
95: if (!$oAccount->save()) {
96: throw new \Aurora\System\Exceptions\ManagerException(0);
97: }
98: } else {
99: throw new \Aurora\System\Exceptions\ManagerException(0);
100: }
101: }
102:
103: $bResult = true;
104: } catch (\Aurora\System\Exceptions\BaseException $oException) {
105: $bResult = false;
106: $this->setLastException($oException);
107: }
108:
109: return $bResult;
110: }
111:
112: /**
113: * @param \Aurora\Modules\OAuthIntegratorWebclient\Models\OauthAccount &$oAccount
114: *
115: * @return bool
116: */
117: public function updateAccount(OauthAccount &$oAccount)
118: {
119: $bResult = false;
120: try {
121: if ($oAccount->validate()) {
122: if (!$oAccount->save()) {
123: throw new \Aurora\System\Exceptions\ManagerException(0);
124: }
125: }
126:
127: $bResult = true;
128: } catch (\Aurora\System\Exceptions\BaseException $oException) {
129: $bResult = false;
130: $this->setLastException($oException);
131: }
132:
133: return $bResult;
134: }
135:
136: /**
137: * @param int $iIdUser
138: * @param string $sType
139: *
140: * @return bool
141: */
142: public function deleteAccount($iIdUser, $sType, $sEmail)
143: {
144: $bResult = false;
145: try {
146: $oSocial = $this->getAccount($iIdUser, $sType, $sEmail);
147: if ($oSocial) {
148: if (!$oSocial->delete()) {
149: throw new \Aurora\System\Exceptions\ManagerException(0);
150: }
151: $bResult = true;
152: }
153: } catch (\Aurora\System\Exceptions\BaseException $oException) {
154: $bResult = false;
155: $this->setLastException($oException);
156: }
157:
158: return $bResult;
159: }
160:
161: /**
162: * @param int $iIdUser
163: *
164: * @return bool
165: */
166: public function deleteAccountByUserId($iIdUser)
167: {
168: return !!OauthAccount::where('IdUser', $iIdUser)->delete();
169: }
170:
171: /**
172: * @param \Aurora\Modules\OAuthIntegratorWebclient\Models\OauthAccount &$oAccount
173: *
174: * @return bool
175: */
176: public function isExists(OauthAccount $oAccount)
177: {
178: return (OauthAccount::find($oAccount->Id) instanceof OauthAccount);
179: }
180: }
181: