Adldap2 / Adldap2-Laravel

LDAP Authentication & Management for Laravel
MIT License
911 stars 184 forks source link

Sync Attribute in ADLDAP2 #176

Closed lalyn closed 8 years ago

lalyn commented 8 years ago

Hi there, could anyone assist me on how to sync employee id attribute in adldap2. I have followed the documentation but my phpmyadmin is not fetching the employee id from AD. I have made changes in ldap_auth.php , ldapattributehandler.php and create_users_table.php migration file. The code is as below :

  1. Migration file :
<?php

use Illuminate\Database\Schema\Blueprint;
use Illuminate\Database\Migrations\Migration;

class CreateUsersTable extends Migration
{
    /**
     * Run the migrations.
     *
     * @return void
     */
    public function up()
    {
        Schema::create('users', function (Blueprint $table) {
            $table->increments('id');
            $table->string('name');
            $table->string('employeeid')->unique();
            $table->string('username');
            $table->string('email')->unique();
            $table->string('password')->nullable();
            $table->string('confirmation_code');
            $table->boolean('confirmed')->default(config('access.users.confirm_email') ? false : true);
            $table->rememberToken();
            $table->timestamp('created_at')->default(DB::raw('CURRENT_TIMESTAMP'));
            $table->timestamp('updated_at');
            $table->softDeletes();
        });
    }

    /**
     * Reverse the migrations.
     *
     * @return void
     */
    public function down()
    {
        Schema::drop('users');
    }
}
  1. LdapAttributeHandler.php
<?php

namespace Adldap\Laravel\Tests\Handlers;

use Adldap\Models\User;

class LdapAttributeHandler
{
    /**
     * Returns the common name of the AD User.
     *
     * @param User $user
     *
     * @return string
     */
    public function name(User $user)
    {
        return $user->getEmployeeId();
//        return 'handled';
    }
}

3.Adldap_auth.php

'sync_attributes' => [

        'name' => 'cn',
        'email' => 'mail',
        'employeeid' => ['employeeID' => 'samaccountname'], 
],

Can anyone help me please since im very new to laravel. I am developing an attendance system for my company.

stevebauman commented 8 years ago

Hi @lalyn, there's a couple issues with the code you've posted.

First of all, delete your LdapAttributeHandler. It's not needed here (nor is it used anywhere).

Next, in your config/adldap_auth.php file, inside the sync_attributes array, use the following array:

'sync_attributes' => [

    'name' => 'cn',
    'email' => 'mail',
    'employeeid' =>  'samaccountname',

],

Now, upon logging in, you should now be able to access your users synchronized samaccountname attribute:

// resources/views/welcome.blade.php

{{ Auth::user()->employeeid }}
lalyn commented 8 years ago

Hi thanks alot for the reply. It was quite helpful. But the error I'm getting now is, the Employee id column in Users table is fetching 'username' not the 'employeeid' itself. Just curious to ask, do I need to create any new migration to fetch this employeeid from AD. Here I have the screenshot for u. image

And Im sorry to ask can u kindly teach me where do I insert this code : // resources/views/welcome.blade.php

{{ Auth::user()->employeeid }}

lalyn commented 8 years ago

My main idea is to tie employee id from adldap2 with the fingerprint database's employee id so that employees will be able to view their own attendance based on employee id (unique key)

stevebauman commented 8 years ago

Oh, if you want the employee ID from Active Directory, just use the configuration:

'sync_attributes' => [

    'name' => 'cn',
    'email' => 'mail',
    'employeeid' =>  'employeeid',

],
lalyn commented 8 years ago

Wow, it works now! Thank you so much, I was struggling with this for almost two days. Thank you really appreciate your help!

lalyn commented 8 years ago

Hi if you dont mind could u please guide me on the query to tie employee id from adldap2 with my fingerprint system's database employee id. Here is my code :

class AttendanceController extends Controller
{
    /**
     * Show a list of all of the articles in the database.
     * @return Response
     */
    public function index()
    {

        return view('backend.attendance.index');
    }

    public function viewOwnAttendance()
    {
        if (access()->hasPermission('view-own-attendance'))
        {
            return view('backend.attendance.viewownattendance');
        }
    }

    public function getOwn(Request $request) 
    {
        if (access()->hasPermission('view-own-attendance'))
        {
            $attendance = DB::connection('mysql2')
                            ->table('user_info')
                            ->leftjoin('attendance','user_info.userid','=','attendance.userid')
                            ->select(DB::raw("user_info.userid,user_info.username, attendance.date, attendance.att_in, attendance.att_out"))
                            ->where('attendance.date', '>=', DB::raw("(DATE_FORMAT(user_info.lastupdate,'%Y-%m-%d'))"))
                            ->where('user_info.username', '=', Auth::user()->name);

            return Datatables::of($attendance)
                    ->filter(function ($query) use ($request) {
                        if($request->has('start_date')) {
                            $query->where('attendance.date', '>=', "{$request->get('start_date')}");
                        }
                        if($request->has('end_date')) {
                            $query->where('attendance.date', '<=', "{$request->get('end_date')}");
                        }
                   })
                    ->make(true); 
        }
    }
stevebauman commented 8 years ago

Hi @lalyn, just curious, why aren't you using laravel models for retreiving user info? You're already using one for authentication.

But to answer your question, the employeeid is synchronized on your users database table, so you could retrieve the current users employeeid using:

Auth::user()->employeeid