forked from eahlys/EdPaste
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathUser.php
71 lines (60 loc) · 1.89 KB
/
User.php
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
<?php
namespace App;
use Illuminate\Notifications\Notifiable;
use Illuminate\Foundation\Auth\User as Authenticatable;
use Illuminate\Support\Facades\Log;
class User extends Authenticatable
{
use Notifiable;
/**
* The attributes that are mass assignable.
*
* @var array
*/
protected $fillable = [
'name', 'email', 'password',
];
/**
* The attributes that should be hidden for arrays.
*
* @var array
*/
protected $hidden = [
'password', 'remember_token',
];
public function pastes()
{
return $this->hasMany('App\Paste', 'userId');
}
public static function create_if_absent($username) {
$user = User::where('name', $username)->first();
if ($user == null) {
Log::debug('Inserting new user.', ['username' => $username]);
$user = User::create([
'name' => $username,
'email' => $username .'@example.local',
'password' => '',
]);
Log::info('User created.', ['user' => $user]);
}
return $user;
}
/**
* This stupid function exists because I couldn't find how to
* properly implement CAS authentication as a Facade for Auth.
* When I have time I'll try to read all Laravel doc and find out.
*/
public static function getCurrentUser() {
cas()->isAuthenticated(); // XXX workaround CAS_OutOfSequenceBeforeAuthenticationCallException (because I don't know how to use Laravel properly)
$username = cas()->getCurrentUser();
$user = User::where('name', $username)->first();
if ($user == null) {
$user = User::create_if_absent($username);
}
return $user;
}
public static function is_owner($paste) {
$user = User::getCurrentUser();
return (($user->id == $paste->userId && $paste->userId != 0)) ? true : false;
}
}