dev-master
9999999-devPHP Apache2 Basic Auth
MIT
The Development Requires
v1.0.0
1.0.0.0PHP Apache2 Basic Auth
MIT
The Development Requires
Wallogit.com
2017 © Pedro Peláez
PHP Apache2 Basic Auth
A really simple class service for .htaccess Basic Auth using .htpasswd and .htgroups files., (*1)
Original HtPasswd and HtGroup classes from Yang Yang, (*2)
If you want a running app using this lib, see PHP Apache2 Basic Auth Manager, (*3)
See API, (*4)
This standard can be installed with the Composer dependency manager., (*5)
composer require rafaelgou/php-apache2-basic-auth
You need to define a .htpasswd file and a optionally a .htgroups file., (*6)
<?php
// ...
use Apache2BasicAuth\Service as HTService;
$htService = new HTService(
'PATH_TO/.htpasswd',
'PATH_TO/.htgroups'
);
<?php
// ... Instanciate the Service ...
// Create a new group
$group = $htService->createGroup();
$group->setName('admin')
->addUser('mateus')
->addUser('tiago');
// Groups can be also set:
$group->setUsers(array('bernardo', 'larissa'));
// Staging to write
$htService->persist($group);
// Writing to disc
$htService->write();
<?php
// ... Instanciate the Service ...
// Create a new group
$group = $htService->findGroup('admin');
$group
->removeUser('mateus')
->addUser('ana');
// Staging to write
$htService->persist($group);
// Writing to disc
$htService->write();
<?php
// ... Instanciate the Service ...
// Create a new group
$group = $htService->findGroup('admin');
// Staging to write
$htService->htService->removeGroup($group);
// Writing to disc
$htService->write();
<?php
// ... Instanciate the Service ...
// Create a new user
$user = $htService->createUser();
$user->setUsername('rafael')
->setPassword('my_pass_123')
->setAddGroup('admin')
->setAddGroup('finance');
// Groups can be also set:
$user->setGroups(array('opperations', 'support'));
// Staging to write
$htService->persist($user);
// Writing to disc
$htService->write();
OBS: When you set a password the hashed password is updated., (*7)
<?php
// ... Instanciate the Service ...
// Finding an existing user
$user = $htService->findUser('rafael');
$user->setPassword('my_pass_123')
->removeGroup('admin')
->setAddGroup('marketing');
// Staging to write
$htService->persist($user);
// Writing to disc
$htService->write();
OBS: When you set a password the hashed password is updated. If not set, old hash is kept., (*8)
<?php
// ... Instanciate the Service ...
// Finding an existing user
$user = $htService->findUser('rafael');
// Staging to write
$htService->htService->removeUser($user);
// Writing to disc
$htService->write();
<?php
// ... Instanciate the Service ...
// Getting Users
$users = $htService->getUsers();
// The key is the username too
foreach ($users as $username => $user) {
echo $user->getUsername();
echo $user->getHash(); // Hashed password
echo implode(', ', $user->getGroups());
}
// Getting Groups
$groups = $htService->getGroups();
// The key is the username too
foreach ($groups as $groupname => $group) {
echo $group->getName();
echo implode(', ', $user->getUsers());
}
PHP Apache2 Basic Auth
MIT
PHP Apache2 Basic Auth
MIT