2017 © Pedro Peláez
 

project php-apache2-basic-auth

PHP Apache2 Basic Auth

image

rafaelgou/php-apache2-basic-auth

PHP Apache2 Basic Auth

  • Thursday, February 16, 2017
  • by rafaelgou
  • Repository
  • 1 Watchers
  • 2 Stars
  • 180 Installations
  • PHP
  • 0 Dependents
  • 0 Suggesters
  • 1 Forks
  • 1 Open issues
  • 2 Versions
  • 7 % Grown

The README.md

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)

Full API

See API, (*4)

Installation

This standard can be installed with the Composer dependency manager., (*5)

  1. Install Composer
  2. Install the coding standard as a dependency of your project
composer require rafaelgou/php-apache2-basic-auth

Using

You need to define a .htpasswd file and a optionally a .htgroups file., (*6)

Instanciate the Service

<?php

// ...
use Apache2BasicAuth\Service as HTService;

$htService = new HTService(
    'PATH_TO/.htpasswd',
    'PATH_TO/.htgroups'
);

Creating a Group

<?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();

Editing a Group

<?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();

Deleting a Group

<?php

// ... Instanciate the Service ...

// Create a new group
$group = $htService->findGroup('admin');

// Staging to write    
$htService->htService->removeGroup($group);

// Writing to disc
$htService->write();

Creating a User

<?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)

Editing a user

<?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)

Deleting a user

<?php

// ... Instanciate the Service ...

// Finding an existing user
$user = $htService->findUser('rafael');

// Staging to write    
$htService->htService->removeUser($user);

// Writing to disc
$htService->write();

Listing Groups and Users

<?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());
}

The Versions

16/02 2017

dev-master

9999999-dev

PHP Apache2 Basic Auth

  Sources   Download

MIT

The Development Requires

01/09 2016