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