AuthController.php 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566
  1. <?php
  2. namespace App\Controllers\Auth;
  3. use App\Controllers\Controller;
  4. use App\Web\Session;
  5. use App\Web\ValidationHelper;
  6. use Psr\Http\Message\ServerRequestInterface as Request;
  7. abstract class AuthController extends Controller
  8. {
  9. protected function checkRecaptcha(ValidationHelper $validator, Request $request)
  10. {
  11. $validator->callIf($this->getSetting('recaptcha_enabled') === 'on', function (Session $session) use (&$request) {
  12. $recaptcha = json_decode(file_get_contents('https://www.google.com/recaptcha/api/siteverify?secret='.$this->getSetting('recaptcha_secret_key').'&response='.param($request, 'recaptcha_token')));
  13. if ($recaptcha->success && $recaptcha->score < 0.5) {
  14. $session->alert(lang('recaptcha_failed'), 'danger');
  15. return false;
  16. }
  17. return true;
  18. });
  19. return $validator;
  20. }
  21. /**
  22. * @return bool|false|resource
  23. */
  24. public function ldapConnect()
  25. {
  26. if (!extension_loaded('ldap')) {
  27. $this->logger->error('The LDAP extension is not loaded.');
  28. return false;
  29. }
  30. $server = ldap_connect($this->config['ldap']['host'], $this->config['ldap']['port']);
  31. if ($server) {
  32. ldap_set_option($server, LDAP_OPT_PROTOCOL_VERSION, 3);
  33. ldap_set_option($server, LDAP_OPT_REFERRALS, 0);
  34. ldap_set_option($server, LDAP_OPT_NETWORK_TIMEOUT, 10);
  35. }
  36. return $server;
  37. }
  38. /**
  39. * @param string $username
  40. * @return string
  41. */
  42. protected function getLdapRdn(string $username)
  43. {
  44. $bindString = ($this->config['ldap']['rdn_attribute'] ?? 'uid=').addslashes($username);
  45. if ($this->config['ldap']['user_domain'] !== null) {
  46. $bindString .= ','.$this->config['ldap']['user_domain'];
  47. }
  48. if ($this->config['ldap']['base_domain'] !== null) {
  49. $bindString .= ','.$this->config['ldap']['base_domain'];
  50. }
  51. return $bindString;
  52. }
  53. }