-
-
Notifications
You must be signed in to change notification settings - Fork 7
Expand file tree
/
Copy pathTeam.php
More file actions
140 lines (120 loc) · 3.46 KB
/
Team.php
File metadata and controls
140 lines (120 loc) · 3.46 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
<?php
declare(strict_types=1);
namespace GeneaLabs\LaravelGovernor;
use GeneaLabs\LaravelGovernor\Relations\TeamMembersRelation;
use GeneaLabs\LaravelGovernor\Traits\Governable;
use Illuminate\Database\Eloquent\Builder;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Relations\BelongsTo;
use Illuminate\Database\Eloquent\Relations\BelongsToMany;
use Illuminate\Database\Eloquent\Relations\HasMany;
class Team extends Model
{
use Governable;
protected $rules = [
'name' => 'required|min:3',
'description' => 'string',
];
protected $fillable = [
'name',
'description',
];
protected $table = "governor_teams";
public function invitations(): HasMany
{
return $this->hasMany(
config('genealabs-laravel-governor.models.invitation'),
"team_id"
);
}
/**
* @deprecated Use governorOwner() relationship instead.
*/
public function owner(): BelongsTo
{
$authClass = config("genealabs-laravel-governor.models.auth");
return $this->belongsTo($authClass, "governor_owned_by", "id");
}
public function members(): BelongsToMany
{
return $this->belongsToMany(
config('genealabs-laravel-governor.models.auth'),
"governor_team_user",
"team_id",
"user_id"
);
}
protected function newBelongsToMany(
Builder $query,
Model $parent,
$table,
$foreignPivotKey,
$relatedPivotKey,
$parentKey,
$relatedKey,
$relationName = null,
): BelongsToMany {
if ($table === 'governor_team_user') {
return new TeamMembersRelation(
$query,
$parent,
$table,
$foreignPivotKey,
$relatedPivotKey,
$parentKey,
$relatedKey,
$relationName,
);
}
return parent::newBelongsToMany(
$query,
$parent,
$table,
$foreignPivotKey,
$relatedPivotKey,
$parentKey,
$relatedKey,
$relationName,
);
}
public function permissions(): HasMany
{
return $this->hasMany(
config('genealabs-laravel-governor.models.permission')
);
}
public function removeMember(Model $user): void
{
$this->members()->detach($user);
}
public function getOwnerNameAttribute(): string
{
return $this->governorOwner?->owner?->name
?? $this->owner?->name
?? "";
}
public function transferOwnership(Model $newOwner): self
{
$this->loadMissing('members');
// Update polymorphic ownership
$ownableClass = config(
"genealabs-laravel-governor.models.ownable",
\GeneaLabs\LaravelGovernor\GovernorOwnable::class,
);
(new $ownableClass)->updateOrCreate(
[
'ownable_type' => get_class($this),
'ownable_id' => $this->getKey(),
],
[
'user_id' => $newOwner->getKey(),
],
);
// Deprecated: maintain governor_owned_by for backward compatibility
$this->governor_owned_by = $newOwner->getKey();
$this->save();
// Clear cached relationship
$this->unsetRelation('governorOwner');
return $this;
}
}