Wednesday, 6 January 2016

Zend Framework 2 Zend Email and Zend _Log


Zend Framework 2 Zend Email and Zend _Log

Question: What is Zend Log?
Zend_Log is a component used for logging in output, database, file, firebug and email etc.


Question: What are different objects in Zend Log?
Following are object which are used zend_log.
  1. A Logger: It is instance of Zend\Log\Logger is the main object for Zend Log.
  2. A Writer: It is inherits from Zend\Log\Writer\AbstractWriter and used to save the log.
  3. A Filter: It is implements Zend\Log\Filter\FilterInterface and used to NOT save the log.
  4. A Formatter: It is implements Zend\Log\Formatter\FormatterInterface and used for Formating when gives to writer.



Question: What are different priorites in Zend_log?
EMERG   = 0;  // Emergency: system is unusable
ALERT   = 1;  // Alert: action must be taken immediately
CRIT    = 2;  // Critical: critical conditions
ERR     = 3;  // Error: error conditions
WARN    = 4;  // Warning: warning conditions
NOTICE  = 5;  // Notice: normal but significant condition
INFO    = 6;  // Informational: informational messages
DEBUG   = 7;  // Debug: debug messages



Question: How to log the data? Give an simple example?
$logger = new \Zend\Log\Logger;
$writer = new \Zend\Log\Writer\Stream('php://output');
$logger->addWriter($writer);
$logger->log(\Zend\Log\Logger::INFO, 'Informational message');



Question: How to distroy the log?
$logger = null;



Question: Can use multiple writer?Give an Example?
Yes, We can do.
See Example Below:
$logger = new \Zend\Log\Logger;
$writer1 = new \Zend\Log\Writer\Stream('php://output');
$writer2 = new \Zend\Log\Writer\Stream('/path/to/second/logfile');
$logger->addWriter($writer1);
$logger->addWriter($writer2);
$logger->log(\Zend\Log\Logger::INFO, 'Informational message');



Question: Can use we use Filter?Give an Example?
$logger = new \Zend\Log\Logger;
$writer = new \Zend\Log\Writer\Stream('php://output');
$logger->addWriter($writer);

$filter = new Zend\Log\Filter\Priority(Logger::CRIT); /** Write only for critical **/
$writer->addFilter($filter);

$logger->log(\Zend\Log\Logger::INFO, 'Informational message');



Question: What are different filter available in Zend_log?
  1. Priority
  2. Regex
  3. Timestamp
  4. SuppressFilter
  5. Validator



Question: What are different Formater available in Zend_log?
  1. Simple Formater
  2. Formatting to XML
  3. Formatting to FirePhp


Question: What is Zend_Email?
Zend_Email is component which is used to send email.


Question: Give an Example of Zend_Email?
use \Zend\Mail;
$mail = new Mail\Message();
$mail->setBody('This is email content.');
$mail->setFrom('sender@example.com', 'Email From');
$mail->addTo('receiver@example.com', 'Receiver');
$mail->setSubject('This is email subject');

$transport = new \Mail\Transport\Sendmail();
$transport->send($mail);



Question: What is Zend\Mail\Transport?
Zend\Mail\Transport is interface which send an email in actual. This interface has only one method i.e send().


Question: How can we send an email using SMTP Transport?
$transport = new SmtpTransport();
$options   = new SmtpOptions(array(
    'name'              => 'localhost.localdomain',
    'host'              => '127.0.0.1',
    'connection_class'  => 'login',
    'connection_config' => array(
        'username' => 'username',
        'password' => 'password',
    ),
));
$transport->setOptions($options);

$transport->send( $mail);



Question: Can we use transport to store email in File?
Yes, We can do.


Tuesday, 5 January 2016

Zend Framework 2 Authentication and Authorization

Zend Framework 2 Authentication and Authorization

Question: What is difference between Authentication and Authorization?
Authentication is the process of determining whether someone is to be identified Or Not. In another words, whether he can login in system OR Not. Authorization is the process of determining whether he can access that particular things OR Not. In another words what section he can access OR Not. Authorization comes after the Authentication.


Question: How to set Secure Authentication?
  1. Login credentials should be encrypted like Md5 during authorization
  2. credentials must not keept in Session
  3. Protected Website from Session Hijacking and Session Fixation
  4. In Login Form use captcha


Question: What are four method available in Zend\Authentication\Result?
  1. isValid()
  2. getCode()
  3. getIdentity()
  4. getMessages()


Question: What constants are return from Zend\Authentication\Result?
  1. Result::SUCCESS
  2. Result::FAILURE
  3. Result::FAILURE_IDENTITY_NOT_FOUND
  4. Result::FAILURE_IDENTITY_AMBIGUOUS
  5. Result::FAILURE_CREDENTIAL_INVALID
  6. Result::FAILURE_UNCATEGORIZED



Question: How to change the Session NameSpace for Auth?
$auth->setStorage(new SessionStorage('someNamespace'));


Question: Can we create "Custom Storage Class" and use in Auth?
Yes, We can create our custom storage class using interface Zend\Authentication\Storage\StorageInterface. For Example:
use Zend\Authentication\Storage\StorageInterface;
class My\Storage implements StorageInterface{
}


Question: Can we create "Custom Adapters" for Auth?
Yes, We can create our custom storage class using interface Zend\Authentication\Adapter\AdapterInterfac. For Example:
use Zend\Authentication\Adapter\AdapterInterface;
class My\Auth\Adapter implements AdapterInterface{
}



Question: How to make user logout from application?
$auth->clearIdentity();



Question: What are different Adapter availble in Zend Framework2.4 for Authentication?
  1. Zend\Authentication\Adapter\DbTable
  2. Zend\Authentication\Adapter\Digest
  3. Zend\Authentication\Adapter\Http
  4. Zend\Authentication\Adapter\Ldap
  5. Zend\Authentication\Adapter\Http\FileResolver



Question: What is ACL component?
The Zend\Permissions\Acl component provides a lightweight & flexible access control list implementation for managing the access to different users.


Question: What is Resource and Role in ACL?
Resource: Resource is an object to which access is controlled. For Example Car.
Zend\Permissions\Acl\Resource\ResourceInterface is available.


Role: Role is an object that may request access to a Resource. For Example Driver
Driver can request to car means An Role can request access to a Resource.
Zend\Permissions\Acl\Role\RoleInterface is available.


Question: What is Rbac component?
Rbac component is a lightweight ACL implementation based around PHP 5.3's SPL RecursiveIterator & RecursiveIteratorIterator.
Its similar to Zend ACL component.
Rbac emphasis on roles and their permissions rather than objects/resources.