Skip to content

Commit b1d5fd6

Browse files
authored
Merge pull request #569 from bakaphp/hotfix-0.3-company-group
[0.3] Hotfix Company Group
2 parents 0b817f7 + b8c22ec commit b1d5fd6

File tree

9 files changed

+79
-31
lines changed

9 files changed

+79
-31
lines changed

composer.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
"require": {
1010
"php": ">=7.4",
1111
"ext-phalcon": ">=4",
12-
"baka/baka": "^0.6",
12+
"baka/baka": "^0.7@dev",
1313
"dariuszp/cli-progress-bar": "^1.0",
1414
"elasticsearch/elasticsearch": "^7.5",
1515
"firebase/php-jwt": "^5.0",

src/Api/Controllers/RolesController.php

Lines changed: 50 additions & 3 deletions
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/UsersInviteController.php

Lines changed: 6 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -8,11 +8,11 @@
88
use Baka\Http\Exception\UnprocessableEntityException;
99
use Baka\Validation as CanvasValidation;
1010
use Canvas\Auth\Auth;
11+
use Canvas\Contracts\AuthTrait;
1112
use Canvas\Models\Roles;
1213
use Canvas\Models\Users;
1314
use Canvas\Models\UsersInvite;
1415
use Canvas\Notifications\Invitation;
15-
use Canvas\Contracts\AuthTrait;
1616
use Exception;
1717
use Phalcon\Http\Response;
1818
use Phalcon\Security\Random;
@@ -107,15 +107,6 @@ public function insertInvite() : Response
107107
//validate this form for password
108108
$validation->validate($request);
109109

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

@@ -204,15 +195,11 @@ public function processUserInvite(string $hash) : Response
204195
//move to DTO
205196
$newUser->password = null;
206197

207-
if (!defined('API_TESTS')) {
208-
$usersInvite->softDelete();
209-
210-
return $this->response([
211-
'user' => $newUser,
212-
'session' => $authInfo
213-
]);
214-
}
198+
$usersInvite->softDelete();
215199

216-
return $this->response($newUser);
200+
return $this->response([
201+
'user' => $newUser,
202+
'session' => $authInfo
203+
]);
217204
}
218205
}

src/Contracts/AuthTrait.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
use Canvas\Auth\Auth;
88
use Canvas\Models\Sessions;
99
use Canvas\Models\Users;
10+
use Phalcon\Http\Response;
1011

1112
trait AuthTrait
1213
{

src/Models/Companies.php

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -228,10 +228,7 @@ public function getSubscription() : Subscription
228228
public function getDefaultCompanyGroup() : CompaniesGroups
229229
{
230230
$companyGroup = $this->getCompanyGroups([
231-
'conditions' => 'Canvas\Models\CompaniesGroups.apps_id = :apps_id: AND Canvas\Models\CompaniesGroups.is_default = 1',
232-
'bind' => [
233-
'apps_id' => Di::getDefault()->get('app')->getId()
234-
],
231+
'conditions' => 'Canvas\Models\CompaniesGroups.is_default = 1',
235232
'limit' => 1
236233
])->getFirst();
237234

src/Models/Roles.php

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@
1616
class Roles extends AbstractModel
1717
{
1818
public string $name;
19-
public ?string$description;
19+
public ?string $description;
2020
public ?int $scope;
2121
public int $companies_id;
2222
public int $apps_id;
@@ -51,6 +51,16 @@ public function initialize()
5151
'roles_id',
5252
['alias' => 'accessList']
5353
);
54+
55+
$this->hasMany(
56+
'id',
57+
UserRoles::class,
58+
'roles_id',
59+
[
60+
'alias' => 'users',
61+
'conditions' => 'is_delete = 0'
62+
]
63+
);
5464
}
5565

5666
/**
@@ -310,7 +320,7 @@ public function afterUpdate()
310320
//if we deleted the role
311321
if ($this->is_deleted) {
312322
//delete
313-
foreach ($this->accesList as $access) {
323+
foreach ($this->accessList as $access) {
314324
$access->softDelete();
315325
}
316326
}

src/Models/UsersInvite.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -113,7 +113,7 @@ public static function isValid(string $email, int $roleId = 1) : bool
113113
/**
114114
* Given the form request return a new user invite.
115115
*
116-
* @param array $requets
116+
* @param array $request
117117
*
118118
* @return Users
119119
*/

storage/db/seeds/InitGewaer.php

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -200,6 +200,8 @@ public function run()
200200
'scope' => 0,
201201
'companies_id' => 1,
202202
'apps_id' => 1,
203+
'is_default' => 1,
204+
'is_active' => 1,
203205
'created_at' => date('Y-m-d H:i:s'),
204206
'is_deleted' => 0
205207
], [
@@ -208,6 +210,8 @@ public function run()
208210
'scope' => 0,
209211
'companies_id' => 1,
210212
'apps_id' => 1,
213+
'is_default' => 1,
214+
'is_active' => 1,
211215
'created_at' => date('Y-m-d H:i:s'),
212216
'is_deleted' => 0
213217
], [
@@ -216,6 +220,8 @@ public function run()
216220
'scope' => 0,
217221
'companies_id' => 1,
218222
'apps_id' => 1,
223+
'is_default' => 1,
224+
'is_active' => 1,
219225
'created_at' => date('Y-m-d H:i:s'),
220226
'is_deleted' => 0
221227
]

tests/api/UsersInviteCest.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@ public function insertInvite(ApiTester $I) : void
5252
$response = $I->grabResponse();
5353
$dataInvite = json_decode($response, true);
5454

55-
$I->assertTrue($dataInvite['email'] == $testEmail);
55+
$I->assertTrue($dataInvite['user']['email'] == $testEmail);
5656
}
5757

5858
/**

0 commit comments

Comments
 (0)