Skip to content

Commit eadfa8e

Browse files
authored
Merge pull request #575 from bakaphp/branch-0.2.2
[0.2] Update to 0.3
2 parents 318b642 + 1771a02 commit eadfa8e

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

64 files changed

+3684
-120
lines changed

.github/workflows/test.yml

+10-2
Original file line numberDiff line numberDiff line change
@@ -108,6 +108,14 @@ jobs:
108108
- name: Phinx Migrate Kanvas Seed
109109
run: vendor/bin/phinx seed:run
110110

111-
- name: Run all test suites
111+
- name: Run Integration
112112
if: success()
113-
run: vendor/bin/codecept run
113+
run: vendor/bin/codecept run integration
114+
115+
- name: Run Unit
116+
if: success()
117+
run: vendor/bin/codecept run unit
118+
119+
- name: Run Api
120+
if: success()
121+
run: vendor/bin/codecept run api

routes/api.php

+2-1
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,8 @@
6161
Route::post('/roles-accesslist/{id}/copy')->controller('RolesAccessListController')->action('copy'),
6262
Route::get('/custom-fields-modules/{id}/fields')->controller('CustomFieldsModulesController')->action('customFieldsByModulesId'),
6363
Route::put('/apps-plans/{id}/method')->controller('AppsPlansController')->action('updatePaymentMethod'),
64-
Route::get('/apps-plans/{id}/method')->controller('PaymentMethodsCredsController')->action('getCurrentPaymentMethodsCreds'),
64+
Route::get('/apps-plans/{id}/method')->controller('PaymentMethodsCredentialsController')->action('getCurrentPaymentMethods'),
65+
//Route::get('/apps-plans/{id}/method')->controller('PaymentMethodsCredsController')->action('getCurrentPaymentMethodsCreds'),
6566
Route::get('/schema/{slug}')->controller('SchemaController')->action('getBySlug'),
6667
Route::get('/schema/{slug}/description')->controller('SchemaController')->action('getModelDescription'),
6768
Route::post('/users/{hash}/change-email')->controller('AuthController')->action('changeUserEmail'),

src/Acl/Manager.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -186,7 +186,7 @@ public function addRole($role, $scope = 0, $accessInherits = null) : bool
186186
if (!RolesDB::exist($role)) {
187187
$rolesDB = new RolesDB();
188188
$rolesDB->name = $role->getName();
189-
$rolesDB->description = $role->getDescription() ?? $role->getName();
189+
$rolesDB->description = $role->getDescription() ?: $role->getName();
190190
$rolesDB->companies_id = $this->getCompany()->getId();
191191
$rolesDB->apps_id = $this->getApp()->getId();
192192
$rolesDB->scope = $scope;

src/Api/Controllers/AppsPlansController.php

+18-16
Original file line numberDiff line numberDiff line change
@@ -4,20 +4,18 @@
44

55
namespace Canvas\Api\Controllers;
66

7-
use Canvas\Models\AppsPlans;
8-
use Stripe\Token as StripeToken;
9-
use Phalcon\Http\Response;
10-
use Stripe\Customer as StripeCustomer;
11-
use Phalcon\Validation\Validator\PresenceOf;
127
use Baka\Http\Exception\NotFoundException;
13-
use Baka\Http\Exception\UnauthorizedException;
148
use Baka\Http\Exception\UnprocessableEntityException;
9+
use Baka\Validation as CanvasValidation;
10+
use Canvas\Models\AppsPlans;
11+
use Canvas\Models\PaymentMethodsCredentials;
1512
use Canvas\Models\Subscription as CanvasSubscription;
16-
use Phalcon\Cashier\Subscription;
1713
use Canvas\Models\UserCompanyApps;
18-
use function Baka\paymentGatewayIsActive;
19-
use Baka\Validation as CanvasValidation;
20-
use Canvas\Models\PaymentMethodsCreds;
14+
use Phalcon\Cashier\Subscription;
15+
use Phalcon\Http\Response;
16+
use Phalcon\Validation\Validator\PresenceOf;
17+
use Stripe\Customer as StripeCustomer;
18+
use Stripe\Token as StripeToken;
2119

2220
/**
2321
* Class LanguagesController.
@@ -70,6 +68,7 @@ public function onConstruct()
7068
* Update a given subscription.
7169
*
7270
* @param string $stripeId
71+
*
7372
* @return Response
7473
*/
7574
public function edit($stripeId) : Response
@@ -127,9 +126,10 @@ public function edit($stripeId) : Response
127126
* Cancel a given subscription.
128127
*
129128
* @param string $stripeId
129+
*
130130
* @return Response
131131
*/
132-
public function delete($stripeId): Response
132+
public function delete($stripeId) : Response
133133
{
134134
$appPlan = $this->model->findFirstByStripeId($stripeId);
135135

@@ -154,9 +154,10 @@ public function delete($stripeId): Response
154154
* Reactivate a given subscription.
155155
*
156156
* @param string $stripeId
157+
*
157158
* @return Response
158159
*/
159-
public function reactivateSubscription($stripeId): Response
160+
public function reactivateSubscription($stripeId) : Response
160161
{
161162
$appPlan = $this->model->findFirstByStripeId($stripeId);
162163

@@ -179,10 +180,12 @@ public function reactivateSubscription($stripeId): Response
179180

180181
/**
181182
* Update payment method.
182-
* @param integer $id
183+
*
184+
* @param int $id
185+
*
183186
* @return Response
184187
*/
185-
public function updatePaymentMethod(string $id): Response
188+
public function updatePaymentMethod(string $id) : Response
186189
{
187190
if (empty($this->request->hasPut('card_token'))) {
188191
$validation = new CanvasValidation();
@@ -235,9 +238,8 @@ public function updatePaymentMethod(string $id): Response
235238
}
236239

237240
if (is_object($stripeCustomer) && $stripeCustomer instanceof StripeCustomer) {
238-
239241
//We now create a partially persist the payment method data
240-
PaymentMethodsCreds::createByStripeToken($token);
242+
PaymentMethodsCredentials::createByStripeToken($token);
241243
return $this->response($subscription);
242244
}
243245
return $this->response('Card could not be updated');

src/Api/Controllers/AuthController.php

+11-6
Original file line numberDiff line numberDiff line change
@@ -6,17 +6,18 @@
66

77
use Baka\Auth\Models\Sessions;
88
use Baka\Auth\Models\Users as BakaUsers;
9+
use Baka\Auth\UserProvider;
910
use Baka\Http\Exception\InternalServerErrorException;
1011
use Baka\Http\Exception\NotFoundException;
1112
use Baka\Validation as CanvasValidation;
1213
use Baka\Validations\PasswordValidation;
1314
use Canvas\Auth\Auth;
1415
use Canvas\Auth\Factory;
1516
use Canvas\Exception\ModelException;
17+
use Canvas\Models\RegisterRoles;
1618
use Canvas\Models\Sources;
1719
use Canvas\Models\UserLinkedSources;
1820
use Canvas\Models\Users;
19-
use Canvas\Models\RegisterRoles;
2021
use Canvas\Notifications\PasswordUpdate;
2122
use Canvas\Notifications\ResetPassword;
2223
use Canvas\Notifications\Signup;
@@ -127,7 +128,7 @@ public function login() : Response
127128
*/
128129
public function signup() : Response
129130
{
130-
$user = $this->userModel;
131+
$user = UserProvider::get();
131132

132133
$request = $this->request->getPostData();
133134

@@ -211,7 +212,7 @@ public function signup() : Response
211212
*/
212213
public function signupByRegisterRole() : Response
213214
{
214-
$user = $this->userModel;
215+
$user = UserProvider::get();
215216

216217
$request = $this->request->getPostData();
217218

@@ -243,7 +244,7 @@ public function signupByRegisterRole() : Response
243244
//validate this form for password
244245
$validation->validate($request);
245246

246-
$registerRole = RegisterRoles::getByUuid($request["roles_uuid"]);
247+
$registerRole = RegisterRoles::getByUuid($request['roles_uuid']);
247248

248249
$user->email = $validation->getValue('email');
249250
$user->firstname = $validation->getValue('firstname');
@@ -309,15 +310,19 @@ public function refresh() : Response
309310
}
310311

311312
//Check if both tokens relate to the same user's email
312-
if ($accessToken->getClaim('sessionId') == $refreshToken->getClaim('sessionId')) {
313+
if ($accessToken->getClaim('sessionId') === $refreshToken->getClaim('sessionId')) {
313314
$user = Users::getByEmail($accessToken->getClaim('email'));
314315
}
315316

316317
if (!$user) {
317318
throw new NotFoundException(_('User not found'));
318319
}
319320

320-
$token = Sessions::restart($user, $refreshToken->getClaim('sessionId'), (string)$this->request->getClientAddress());
321+
$token = Sessions::restart(
322+
$user,
323+
$refreshToken->getClaim('sessionId'),
324+
(string)$this->request->getClientAddress()
325+
);
321326

322327
return $this->response([
323328
'token' => $token['token'],

src/Api/Controllers/IndexController.php

+12
Original file line numberDiff line numberDiff line change
@@ -87,6 +87,18 @@ public function status() : Response
8787
$response['errors']['db'] = "The database isn't working.";
8888
}
8989

90+
if ($this->di->has('dbLocal')) {
91+
try {
92+
$this->dbLocal->connect();
93+
} catch (PDOException $e) {
94+
$this->log->error($e->getMessage(), $e->getTrace());
95+
$response['errors']['dbLocal'] = $e->getMessage();
96+
} catch (Exception $e) {
97+
$this->log->error("The database isn't working. {$e->getMessage()}", $e->getTrace());
98+
$response['errors']['dbLocal'] = "The database isn't working.";
99+
}
100+
}
101+
90102
if (!count($response)) {
91103
return $this->response(['OK']);
92104
}

src/Api/Controllers/PaymentMethodsCredsController.php src/Api/Controllers/PaymentMethodsCredentialsController.php

+4-4
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44

55
namespace Canvas\Api\Controllers;
66

7-
use Canvas\Models\PaymentMethodsCreds;
7+
use Canvas\Models\PaymentMethodsCredentials;
88
use Exception;
99
use Phalcon\Http\Response;
1010

@@ -14,7 +14,7 @@
1414
* @package Canvas\Api\Controllers
1515
*
1616
*/
17-
class PaymentMethodsCredsController extends BaseController
17+
class PaymentMethodsCredentialsController extends BaseController
1818
{
1919
/*
2020
* fields we accept to create
@@ -37,7 +37,7 @@ class PaymentMethodsCredsController extends BaseController
3737
*/
3838
public function onConstruct()
3939
{
40-
$this->model = new PaymentMethodsCreds();
40+
$this->model = new PaymentMethodsCredentials();
4141
$this->additionalSearchFields = [
4242
['is_deleted', ':', '0'],
4343
['users_id', ':', $this->userData->getId()],
@@ -51,7 +51,7 @@ public function onConstruct()
5151
*
5252
* @return Response
5353
*/
54-
public function getCurrentPaymentMethodsCreds() : Response
54+
public function getCurrentPaymentMethods() : Response
5555
{
5656
$paymentMethod = [];
5757

src/Api/Controllers/RolesController.php

+50-3
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,10 @@
44

55
namespace Canvas\Api\Controllers;
66

7-
use Canvas\Models\Roles;
7+
use Baka\Http\Exception\ForbiddenException;
88
use Canvas\Models\Apps;
9+
use Canvas\Models\Roles;
10+
use Phalcon\Http\Response;
911

1012
class RolesController extends BaseController
1113
{
@@ -33,10 +35,55 @@ public function onConstruct()
3335
$this->model = new Roles();
3436

3537
//get the list of roes for the systems + my company
38+
$companyTotalRoles = Roles::count([
39+
'conditions' => 'apps_id = :apps_id: AND companies_id = :companies_id:',
40+
'bind' => [
41+
'apps_id' => $this->acl->getApp()->getId(),
42+
'companies_id' => $this->userData->currentCompanyId(),
43+
]
44+
]);
45+
3646
$this->additionalSearchFields = [
3747
['is_deleted', ':', '0'],
38-
['apps_id', ':', Apps::CANVAS_DEFAULT_APP_ID . '|' . $this->acl->getApp()->getId()],
39-
['companies_id', ':', '1|' . $this->userData->currentCompanyId()],
48+
['apps_id', ':', $this->acl->getApp()->getId()],
49+
['companies_id', ':', $this->userData->currentCompanyId()],
4050
];
51+
52+
if ($companyTotalRoles === 0) {
53+
$this->additionalSearchFields = [
54+
['is_deleted', ':', '0'],
55+
['apps_id', ':', Apps::CANVAS_DEFAULT_APP_ID],
56+
['companies_id', ':', 1],
57+
['is_default', ':', 1],
58+
];
59+
}
60+
}
61+
62+
/**
63+
* Delete a Record.
64+
*
65+
* @throws Exception
66+
*
67+
* @return Response
68+
*/
69+
public function delete($id) : Response
70+
{
71+
$role = $this->getRecordById($id);
72+
73+
if ($role->companies_id === Apps::CANVAS_DEFAULT_APP_ID) {
74+
throw new ForbiddenException('Cant delete a Global App Role');
75+
}
76+
77+
if ($role->getUsers()->count() > 0) {
78+
throw new ForbiddenException('Cant delete a Role in use');
79+
}
80+
81+
if ($this->softDelete) {
82+
$role->softDelete();
83+
} else {
84+
$role->delete();
85+
}
86+
87+
return $this->response(['Delete Successfully']);
4188
}
4289
}

src/Api/Controllers/SessionsController.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -5,12 +5,12 @@
55
namespace Canvas\Api\Controllers;
66

77
use Baka\Auth\Models\Sessions;
8-
use Phalcon\Http\Response;
98

109
/**
1110
* Class LanguagesController.
1211
*
1312
* @package Canvas\Api\Controllers
13+
*
1414
* @property UserData $userData
1515
*
1616
*/

src/Api/Controllers/UsersInviteController.php

+5-16
Original file line numberDiff line numberDiff line change
@@ -107,13 +107,6 @@ public function insertInvite() : Response
107107
//validate this form for password
108108
$validation->validate($request);
109109

110-
//Check if role is not a default one.
111-
if (!Roles::existsById((int)$request['role_id'])->isDefault()) {
112-
throw new UnprocessableEntityException(
113-
"Can't create a new user with a default role."
114-
);
115-
}
116-
117110
//Check if user was already was invited to current company and return message
118111
UsersInvite::isValid($request['email'], (int) $request['role_id']);
119112

@@ -202,15 +195,11 @@ public function processUserInvite(string $hash) : Response
202195
//move to DTO
203196
$newUser->password = null;
204197

205-
if (!defined('API_TESTS')) {
206-
$usersInvite->softDelete();
198+
$usersInvite->softDelete();
207199

208-
return $this->response([
209-
'user' => $newUser,
210-
'session' => $authInfo
211-
]);
212-
}
213-
214-
return $this->response($newUser);
200+
return $this->response([
201+
'user' => $newUser,
202+
'session' => $authInfo
203+
]);
215204
}
216205
}

0 commit comments

Comments
 (0)