Bristol SU Support Package
SessionAuthentication.php
Go to the documentation of this file.
1 <?php
2 
4 
5 use BristolSU\ControlDB\Contracts\Repositories\Role as RoleRepository;
10 use BristolSU\ControlDB\Contracts\Repositories\Group as GroupRepository;
11 use \BristolSU\ControlDB\Contracts\Models\User;
14 
19 {
20 
26  private $session;
27 
40  private $userRepository;
41 
47  private $roleRepository;
48 
49 
58  public function __construct(Session $session, GroupRepository $groupRepository, UserRepository $userRepository, RoleRepository $roleRepository)
59  {
60  $this->session = $session;
61  $this->groupRepository = $groupRepository;
62  $this->userRepository = $userRepository;
63  $this->roleRepository = $roleRepository;
64  }
65 
73  public function getGroup()
74  {
75  if (($role = $this->getRole()) !== null) {
76  return $role->group();
77  }
78 
79  if ($this->session->has('group_id')) {
80  return $this->groupRepository->getById(
81  $this->session->get('group_id')
82  );
83  }
84 
85  return null;
86  }
87 
93  public function getRole()
94  {
95  if ($this->session->has('role_id')) {
96  return $this->roleRepository->getById(
97  $this->session->get('role_id')
98  );
99  }
100 
101  return null;
102  }
103 
109  public function getUser()
110  {
111  if ($this->session->has('user_id')) {
112  return $this->userRepository->getById(
113  $this->session->get('user_id')
114  );
115  }
116 
117  return null;
118  }
119 
126  public function setGroup(Group $group)
127  {
128  $this->session->put('group_id', $group->id());
129  }
130 
137  public function setRole(Role $role)
138  {
139  $this->session->put('role_id', $role->id());
140  }
141 
148  public function setUser(User $user)
149  {
150  $this->session->put('user_id', $user->id());
151  }
152 
158  public function reset(): void
159  {
160  $this->session->forget([
161  'user_id', 'group_id', 'role_id'
162  ]);
163  }
164 }
__construct(Session $session, GroupRepository $groupRepository, UserRepository $userRepository, RoleRepository $roleRepository)