Skip to content

Commit 9ea7f25

Browse files
Remove legacy UserProject model (#2835)
Thanks to a number of recent feature removals, the legacy `UserProject` model is now only used in a handful of locations. This PR replaces all remaining usages of the legacy model with the new preferred method of Eloquent relationship manipulations.
1 parent 4fa9de6 commit 9ea7f25

7 files changed

Lines changed: 42 additions & 365 deletions

File tree

app/Http/Controllers/UserController.php

Lines changed: 5 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,6 @@
1212
use CDash\Model\BuildConfigure;
1313
use CDash\Model\BuildUpdate;
1414
use CDash\Model\Project;
15-
use CDash\Model\UserProject;
1615
use Illuminate\Database\Eloquent\ModelNotFoundException;
1716
use Illuminate\Http\JsonResponse;
1817
use Illuminate\Support\Carbon;
@@ -67,22 +66,18 @@ public function userPageContent(): JsonResponse
6766
}
6867

6968
// Go through the list of project the user is part of.
70-
$UserProject = new UserProject();
71-
$UserProject->UserId = $userid;
72-
$project_rows = $UserProject->GetProjects();
7369
$start = gmdate(FMT_DATETIME, strtotime(date('r')) - (3600 * 24));
7470
$Project = new Project();
7571
$projects_response = [];
76-
foreach ($project_rows as $project_row) {
77-
$eloquent_project = \App\Models\Project::findOrFail((int) $project_row['id']);
78-
$Project->Id = $project_row['id'];
79-
$Project->Name = $project_row['name'];
72+
foreach ($user->projects()->withPivot('role')->get() as $project_row) {
73+
$Project->Id = $project_row->id;
74+
$Project->Name = $project_row->name;
8075
$project_response = [];
8176
$project_response['id'] = $Project->Id;
82-
$project_response['role'] = $project_row['role']; // 0 is normal user, 1 is maintainer, 2 is administrator
77+
$project_response['role'] = $project_row->pivot->role; // 0 is normal user, 2 is administrator
8378
$project_response['name'] = $Project->Name;
8479
$project_response['name_encoded'] = urlencode($Project->Name);
85-
$project_response['nbuilds'] = $eloquent_project->builds()->count();
80+
$project_response['nbuilds'] = $project_row->builds()->count();
8681
$project_response['average_builds'] = round($Project->GetBuildsDailyAverage(gmdate(FMT_DATETIME, time() - (3600 * 24 * 7)), gmdate(FMT_DATETIME)), 2);
8782
$project_response['success'] = $Project->GetNumberOfPassingBuilds($start, gmdate(FMT_DATETIME));
8883
$project_response['error'] = $Project->GetNumberOfErrorBuilds($start, gmdate(FMT_DATETIME));

app/cdash/app/Model/Project.php

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1034,14 +1034,6 @@ public function InitialSetup(): bool
10341034
}
10351035
}
10361036

1037-
// Add administrator to the project.
1038-
$UserProject = new UserProject();
1039-
$UserProject->Role = 2;
1040-
$UserProject->EmailType = 3; // receive all emails
1041-
$UserProject->ProjectId = $this->Id;
1042-
$UserProject->UserId = 1; // administrator
1043-
$UserProject->Save();
1044-
10451037
return true;
10461038
}
10471039

app/cdash/app/Model/UserProject.php

Lines changed: 0 additions & 195 deletions
This file was deleted.

app/cdash/public/api/v1/project.php

Lines changed: 11 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@
2424
use App\Rules\ProjectVisibilityAllowed;
2525
use App\Utils\RepositoryUtils;
2626
use CDash\Model\Project;
27-
use CDash\Model\UserProject;
27+
use App\Models\Project as EloquentProject;
2828
use Illuminate\Support\Facades\Auth;
2929
use Illuminate\Support\Facades\DB;
3030
use Illuminate\Support\Facades\Gate;
@@ -224,16 +224,17 @@ function create_project(&$response, $user)
224224
populate_project($Project);
225225
$Project->InitialSetup();
226226

227+
$eloquent_project = EloquentProject::findOrFail((int) $Project->Id);
228+
227229
// Add the current user to this project.
228-
if ($user->id != 1) {
229-
// Global admin is already added, so no need to do it again.
230-
$UserProject = new UserProject();
231-
$UserProject->UserId = $user->id;
232-
$UserProject->ProjectId = $Project->Id;
233-
$UserProject->Role = 2;
234-
$UserProject->EmailType = 3;// receive all emails
235-
$UserProject->Save();
236-
}
230+
$eloquent_project->users()
231+
->attach($user->id, [
232+
'emailtype' => 3, // receive all emails
233+
'emailcategory' => 126,
234+
'emailsuccess' => false,
235+
'emailmissingsites' => false,
236+
'role' => EloquentProject::PROJECT_ADMIN,
237+
]);
237238

238239
$response['projectcreated'] = 1;
239240
$response['project'] = $Project->ConvertToJSON();

app/cdash/tests/test_authtoken.php

Lines changed: 10 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,10 @@
33
require_once dirname(__FILE__) . '/cdash_test_case.php';
44

55
use App\Models\AuthToken;
6+
use App\Models\Project as EloquentProject;
7+
use App\Models\User;
68
use App\Utils\AuthTokenUtil;
7-
use CDash\Database;
89
use CDash\Model\Project;
9-
use CDash\Model\UserProject;
1010
use GuzzleHttp\Client;
1111
use GuzzleHttp\Exception\ClientException;
1212
use Illuminate\Support\Facades\DB;
@@ -19,13 +19,11 @@ class AuthTokenTestCase extends KWWebTestCase
1919
private $PostBuildId;
2020
private $Project;
2121
private $Hash;
22-
private $PDO;
2322

2423
public function __construct()
2524
{
2625
parent::__construct();
2726
$this->Hash = '';
28-
$this->PDO = Database::getInstance()->getPdo();
2927
$this->PostBuildId = 0;
3028
$this->Project = null;
3129
$this->Token = '';
@@ -58,17 +56,14 @@ public function testEnableAuthenticatedSubmissions()
5856
$this->Project->Id = $projectid;
5957

6058
// Subscribe a non-administrative user to it.
61-
$stmt = $this->PDO->query("SELECT * FROM users WHERE email = 'user1@kw'");
62-
$row = $stmt->fetch();
63-
if (!$row) {
64-
$this->fail('Failed to find non-admin user');
65-
}
66-
$userproject = new UserProject();
67-
$userproject->ProjectId = $projectid;
68-
$userproject->UserId = $row['id'];
69-
if (!$userproject->Save()) {
70-
$this->fail('Failed to assign user to project');
71-
}
59+
EloquentProject::findOrFail((int) $projectid)->users()
60+
->attach(User::where('email', 'user1@kw')->firstOrFail()->id, [
61+
'emailtype' => 3, // receive all emails
62+
'emailcategory' => 126,
63+
'emailsuccess' => false,
64+
'emailmissingsites' => false,
65+
'role' => EloquentProject::PROJECT_USER,
66+
]);
7267
}
7368

7469
public function testGenerateToken()

app/cdash/tests/test_issuecreation.php

Lines changed: 11 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
11
<?php
22

3+
use App\Models\Project as EloquentProject;
34
use App\Models\User;
45
use App\Utils\RepositoryUtils;
56
use CDash\Database;
67
use CDash\Model\Build;
78
use CDash\Model\Project;
8-
use CDash\Model\UserProject;
99

1010
class IssueCreationTestCase extends KWWebTestCase
1111
{
@@ -86,13 +86,15 @@ public function testIssueCreation()
8686
$this->Builds['clean'] = $clean_build;
8787

8888
// Add user1@kw as a project administrator.
89-
$user = User::where('email', '=', 'user1@kw')->first();
90-
$userid = $user->id;
91-
$userproject = new UserProject();
92-
$userproject->UserId = $userid;
93-
$userproject->ProjectId = $this->Projects['CDash']->Id;
94-
$userproject->Role = 2;
95-
$userproject->Save();
89+
$userid = User::where('email', 'user1@kw')->firstOrFail()->id;
90+
EloquentProject::findOrFail((int) $this->Projects['CDash']->Id)->users()
91+
->attach($userid, [
92+
'emailtype' => 3, // receive all emails
93+
'emailcategory' => 126,
94+
'emailsuccess' => false,
95+
'emailmissingsites' => false,
96+
'role' => EloquentProject::PROJECT_ADMIN,
97+
]);
9698

9799
// Setup subprojects.
98100
$file = dirname(__FILE__) . '/data/GithubPR/Project.xml';
@@ -102,7 +104,7 @@ public function testIssueCreation()
102104

103105
// Verify that administrative access to this project was not overwritten
104106
// by the Project.xml handler.
105-
$project = App\Models\Project::findOrFail((int) $this->Projects['CDash']->Id);
107+
$project = EloquentProject::findOrFail((int) $this->Projects['CDash']->Id);
106108
if ($project->administrators()->find($userid) === null) {
107109
$this->fail('Expected role to be an administrator');
108110
}

0 commit comments

Comments
 (0)