Permalink
Browse files

Added support for user state usage and regulate login based on allowe…

…d states
  • Loading branch information...
artur-gajewski committed Sep 11, 2012
1 parent 2920e66 commit 0ef8c633e55fbaddd2773a54a236c56485485afe
@@ -169,6 +169,30 @@ $settings = array(
*/
//'password_cost' => 14,
+ /**
+ * Enable user state usage
+ *
+ * Should user's state be used in the registration/login process?
+ */
+ //'enable_user_state' => true,
+
+ /**
+ * Default user state upon registration
+ *
+ * What state user should have upon registration?
+ * Allowed value type: integer
+ */
+ //'default_user_state' => 1,
+
+ /**
+ * States which are allowing user to login
+ *
+ * When user tries to login, is his/her state one of the following?
+ * Include null if you want user's with no state to login as well.
+ * Allowed value types: null and integer
+ */
+ //'allowed_login_states' => array( null, 1 ),
+
/**
* End of ZfcUser configuration
*/
View
@@ -4,5 +4,6 @@ CREATE TABLE user
username VARCHAR(255) DEFAULT NULL UNIQUE,
email VARCHAR(255) DEFAULT NULL UNIQUE,
display_name VARCHAR(50) DEFAULT NULL,
- password VARCHAR(128) NOT NULL
+ password VARCHAR(128) NOT NULL,
+ state SMALLINT
) ENGINE=InnoDB;
View
@@ -4,5 +4,6 @@ CREATE TABLE user
username VARCHAR(255) DEFAULT NULL UNIQUE,
email VARCHAR(255) DEFAULT NULL UNIQUE,
display_name VARCHAR(50) DEFAULT NULL,
- password VARCHAR(128) NOT NULL
+ password VARCHAR(128) NOT NULL,
+ state SMALLINT
);
@@ -68,7 +68,17 @@ public function authenticate(AuthEvent $e)
$this->setSatisfied(false);
return false;
}
-
+
+ if ($this->getOptions()->getEnableUserState()) {
+ // Don't allow user to login if state is not in allowed list
+ if (!in_array($userObject->getState(), $this->getOptions()->getAllowedLoginStates())) {
+ $e->setCode(AuthenticationResult::FAILURE_INACTIVE)
+ ->setMessages(array('A record with the supplied identity is not active.'));
+ $this->setSatisfied(false);
+ return false;
+ }
+ }
+
$bcrypt = new Bcrypt();
$bcrypt->setCost($this->getOptions()->getPasswordCost());
if (!$bcrypt->verify($credential,$userObject->getPassword())) {
@@ -28,6 +28,11 @@ class User implements UserInterface
* @var string
*/
protected $password;
+
+ /**
+ * @var int
+ */
+ protected $state;
/**
* Get id.
@@ -138,4 +143,26 @@ public function setPassword($password)
$this->password = $password;
return $this;
}
+
+ /**
+ * Get state.
+ *
+ * @return int
+ */
+ public function getState()
+ {
+ return $this->state;
+ }
+
+ /**
+ * Set state.
+ *
+ * @param int $state
+ * @return UserInterface
+ */
+ public function setState($state)
+ {
+ $this->state = $state;
+ return $this;
+ }
}
@@ -78,4 +78,20 @@ public function getPassword();
* @return UserInterface
*/
public function setPassword($password);
+
+ /**
+ * Get state.
+ *
+ * @return int
+ */
+ public function getState();
+
+ /**
+ * Set state.
+ *
+ * @param int $state
+ * @return UserInterface
+ */
+ public function setState($state);
+
}
@@ -43,6 +43,21 @@ class ModuleOptions extends AbstractOptions implements
*/
protected $loginAfterRegistration = true;
+ /**
+ * @var int
+ */
+ protected $enableUserState = false;
+
+ /**
+ * @var int
+ */
+ protected $defaultUserState = 1;
+
+ /**
+ * @var Array
+ */
+ protected $allowedLoginStates = array( null, 1 );
+
/**
* @var array
*/
@@ -244,6 +259,72 @@ public function getLoginAfterRegistration()
return $this->loginAfterRegistration;
}
+ /**
+ * get user state usage for registration/login process
+ *
+ * @return int
+ */
+ public function getEnableUserState()
+ {
+ return $this->enableUserState;
+ }
+
+ /**
+ * set user state usage for registration/login process
+ *
+ * @param boolean $flag
+ * @return ModuleOptions
+ */
+ public function setEnableUserState($flag)
+ {
+ $this->enableUserState = $flag;
+ return $this;
+ }
+
+ /**
+ * get default user state on registration
+ *
+ * @return int
+ */
+ public function getDefaultUserState()
+ {
+ return $this->defaultUserState;
+ }
+
+ /**
+ * set default user state on registration
+ *
+ * @param int $state
+ * @return ModuleOptions
+ */
+ public function setDefaultUserState($state)
+ {
+ $this->defaultUserState = $state;
+ return $this;
+ }
+
+ /**
+ * get list of states to allow user login
+ *
+ * @return int
+ */
+ public function getAllowedLoginStates()
+ {
+ return $this->allowedLoginStates;
+ }
+
+ /**
+ * set list of states to allow user login
+ *
+ * @param Array $states
+ * @return ModuleOptions
+ */
+ public function setAllowedLoginStates(Array $states)
+ {
+ $this->allowedLoginStates = $states;
+ return $this;
+ }
+
/**
* set auth identity fields
*
@@ -82,6 +82,13 @@ public function register(array $data)
if ($this->getOptions()->getEnableDisplayName()) {
$user->setDisplayName($data['display_name']);
}
+
+ // If user state is enabled, set the default state value
+ if ($this->getOptions()->getEnableUserState()) {
+ if ($this->getOptions()->getDefaultUserState()) {
+ $user->setState($this->getOptions()->getDefaultUserState());
+ }
+ }
$this->getEventManager()->trigger(__FUNCTION__, $this, array('user' => $user, 'form' => $form));
$this->getUserMapper()->insert($user);
$this->getEventManager()->trigger(__FUNCTION__.'.post', $this, array('user' => $user, 'form' => $form));

0 comments on commit 0ef8c63

Please sign in to comment.