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