Is it advisable to synchronize all users and organizational units from LDAP to a SQL table for easier rights management in PHP applications?

Synchronizing all users and organizational units from LDAP to a SQL table can be beneficial for easier rights management in PHP applications. This approach allows for centralized user management and simplifies access control within the application. By syncing LDAP data to a SQL table, you can easily query and manage user permissions within your PHP application.

// Connect to LDAP server
$ldapconn = ldap_connect("ldap.example.com");

// Bind to LDAP server
$ldapbind = ldap_bind($ldapconn, "cn=admin,dc=example,dc=com", "password");

// Search for all users and organizational units
$result = ldap_search($ldapconn, "dc=example,dc=com", "(objectClass=*)");
$data = ldap_get_entries($ldapconn, $result);

// Connect to SQL database
$conn = new mysqli("localhost", "username", "password", "database");

// Truncate existing users table
$conn->query("TRUNCATE TABLE users");

// Insert LDAP data into SQL table
foreach ($data as $entry) {
    $username = $entry["cn"][0];
    $organizationalUnit = $entry["ou"][0];
    
    $conn->query("INSERT INTO users (username, organizational_unit) VALUES ('$username', '$organizationalUnit')");
}

// Close LDAP connection
ldap_close($ldapconn);

// Close SQL connection
$conn->close();