Bristol SU Support Package
WebRequestAuthentication.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 ($this->request !== null && $this->request->query->has('g')) {
76  try {
77  return $this->groupRepository->getById((int) $this->request->query->get('g'));
78  } catch (Exception $e) {
79  }
80  }
81  return null;
82  }
83 
89  public function getRole()
90  {
91  if ($this->request !== null && $this->request->query->has('r')) {
92  try {
93  return $this->roleRepository->getById((int) $this->request->query->get('r'));
94  } catch (Exception $e) {
95  }
96  }
97  return null;
98  }
99 
105  public function getUser()
106  {
107  if ($this->request !== null && $this->request->query->has('u')) {
108  try {
109  return $this->userRepository->getById((int) $this->request->query->get('u'));
110  } catch (Exception $e) {
111  }
112  }
113  return null;
114  }
115 
122  public function setGroup(Group $group)
123  {
124  $this->request->query->set('g', $group->id());
125  $this->request->overrideGlobals();
126  }
127 
134  public function setRole(Role $role)
135  {
136  $this->request->query->set('r', $role->id());
137  $this->request->overrideGlobals();
138  }
139 
146  public function setUser(User $user)
147  {
148  $this->request->query->set('u', $user->id());
149  $this->request->overrideGlobals();
150  }
151 
157  public function reset(): void
158  {
159  $this->request->query->remove('u');
160  $this->request->query->remove('g');
161  $this->request->query->remove('r');
162  $this->request->overrideGlobals();
163  }
164 }
__construct(Request $request, RoleRepository $roleRepository, GroupRepository $groupRepository, UserRepository $userRepository)