DirectoryTree / LdapRecord

A fully-featured LDAP framework.
https://ldaprecord.com
MIT License
512 stars 44 forks source link

[Support] IS there any demo app for this plugin? #577

Closed MaciejSuchanski closed 1 year ago

MaciejSuchanski commented 1 year ago

Is there any demo app source code? The on that will only need a AD basic config filled and will work? I struggle to make anything working myself :(

ajibarra commented 1 year ago

It would help if you attach your current config so we can see if there is anything wrong with it.

MaciejSuchanski commented 1 year ago

Things i don't know how to do:

  1. use firstName.lastName username, not FirstName LastName,
  2. make a session or block access to all other pages without loggin in first,
  3. destroy that session after log out or some time or browser close. Code below, i use laravel 10 for frontend and backend (this does log me in):

    $attributes = request()->validate([
        'username' => 'required',
        'password' => 'required',
    ]);
    
    $u = $attributes['username'];
    $p = $attributes['password'];
    
    $new_u = ucwords(Str::replace('.', ' ', $u));
    
    $connection = new Connection([
        'hosts'    => ['192.168.1.1']
    ]);
    
    if ($connection->auth()->attempt($new_u, $p, $stayBound = true)) {
        return redirect('/wyslij')->with('success', 'Logged!');
    } else {
        return redirect('/zaloguj')->with('success', 'Bad username or password!');
    }
ajibarra commented 1 year ago

I cannot help with Laravel stuff because I don't use it but maybe somebody else can help you with it. You could give a try to CakePHP which usually makes things a lot easier.

On the other hand I see you are using $connection->auth directly. I would create connection and then call $connection->connect() to finally add the connection to the Container (LdapRecord\Container) which would allow you to actually execute queries over LDAP etc (i.e getting all logged in user attributes)

About the point 1 I think @stevebauman have clarified that on #572

MaciejSuchanski commented 1 year ago

Laravel is a requirement, tried #572 but it does nothing, i don't understand what that should do. Username is passed exacly the same as the one i type.

    $attributes = request()->validate([
        'username' => 'required',
        'password' => 'required',
    ]);

    $u = $attributes['username'];
    $p = $attributes['password'];

    $escaped = (new EscapedValue($u))->dn();

    $connection = new Connection([
        'hosts'    => ['192.168.1.1']
    ]);

    dd($escaped);

    if ($connection->auth()->attempt($escaped, $p, $stayBound = true)) {
        return redirect('/wyslij')->with('success', 'Zalogowany!');
    } else {
        return redirect('/zaloguj')->with('success', 'Błędne dane!');
    }
MaciejSuchanski commented 1 year ago

Also i don't understand this part you wrote: " On the other hand I see you are using $connection->auth directly. I would create connection and then call $connection->connect() to finally add the connection to the Container (LdapRecord\Container) which would allow you to actually execute queries over LDAP etc (i.e getting all logged in user attributes) "

I just want to check the username and password and if it's correct i want to log in on website nothing else from LDAP is needed.

ajibarra commented 1 year ago

Could you try $connection->connect($username,$password) and paste here the result?

MaciejSuchanski commented 1 year ago

This is how i used it:

    $attributes = request()->validate([
        'username' => 'required',
        'password' => 'required',
    ]);

    $u = $attributes['username'];
    $p = $attributes['password'];

    $new_u = ucwords(Str::replace('.', ' ', $u));

    $connection = new Connection([
        'hosts'    => ['192.168.1.1']
    ]);

    if ($connection->auth()->attempt($new_u, $p, $stayBound = true)) {
        $connection->connect($new_u,$p);
        ddd($connection);
        return redirect('/wyslij')->with('success', 'Zalogowany!');
    } else {
        return redirect('/zaloguj')->with('success', 'Błędne dane!');
    }

This is the result from ddd():

  LdapRecord\Connection {[#489 ▼](http://127.0.0.1:8000/zaloguj#sf-dump-494899255-ref2489)
    #ldap: LdapRecord\Ldap {#505 ▼
      #host: "ldap://192.168.1.1:389"
      #connection: LDAP\Connection {#507}
      #bound: true
      #useSSL: false
      #useTLS: false
    }
    #cache: null
    #configuration: LdapRecord\Configuration\DomainConfiguration {#501 ▼
      #options: array:11 [▼
        "hosts" => array:1 [▼
          0 => "192.168.1.1"
        ]
        "timeout" => 5
        "version" => 3
        "port" => 389
        "base_dn" => ""
        "username" => ""
        "password" => ""
        "use_ssl" => false
        "use_tls" => false
        "follow_referrals" => false
        "options" => []
      ]
    }
    #dispatcher: null
    #host: "192.168.1.1"
    #hosts: & array:1 [▼
      0 => "192.168.1.1"
    ]
    #attempted: []
    #failed: Closure() {#502 ▼
      class: "LdapRecord\Connection"
      this: LdapRecord\Connection {[#489](http://127.0.0.1:8000/zaloguj#sf-dump-494899255-ref2489)}
      file: "xxx"
      line: "100 to 102"
    }
    #authGuardResolver: Closure() {#504 ▼
      class: "LdapRecord\Connection"
      this: LdapRecord\Connection {[#489](http://127.0.0.1:8000/zaloguj#sf-dump-494899255-ref2489)}
      file: "xxx"
      line: "104 to 106"
    }
    #retryingInitialConnection: false
  }

It still log me in.

ajibarra commented 1 year ago

I don't see any error, even the connection is bound which means it could connect and authenticate to server. So I feel you have issues with Laravel stuff and as I said I don't use it..maybe somebody else will be able to help.

MaciejSuchanski commented 1 year ago

Ok thank you for your time.

stevebauman commented 1 year ago

Hi @MaciejSuchanski,

I don't provide starters, as everyone has their own preferences of what Laravel starter kit should be used and how it should be implemented. I would also likely receive requests for variations of those starters (Laravel Breeze, Laravel JetStream, Laravel Auth, Plain Laravel), and I wouldn't have the time to maintain these.

If you're looking for something simple to get started with, I would recommend starting a small demo project with Laravel UI with the Bootstrap scaffolding, even though it's older than Breeze and Jetstream. It's very straightforward to get up and running with very little compilation steps and a simpler backend structure. I have a step-by-step guide for Laravel UI in the documentation here:

https://ldaprecord.com/docs/laravel/v2/auth/database/laravel-ui#introduction

Laravel Breeze and Jetstream include a lot of functionality out-of-the-box, but may cause confusion for a newcomer to the Laravel due to all their files, options, and compilation steps.

I hope this helps! Good luck! 🙏