我有以下代码,但有效,但现在是下一步。
我必须如何以及在何处设置会话,以便脚本“看到”用户已经登录?
if ($form->isValid()) {
$securePass = $this->getUsersTable()->getUserByUsername( $this->params()->fromPost('username') );
if( $securePass ){
$bcrypt = new Bcrypt();
if ($bcrypt->verify( $this->params()->fromPost('password') , $securePass->password)) {
$sm = $this->getServiceLocator();
$dbAdapter = $sm->get('Zend\Db\Adapter\Adapter');
$authAdapter = new AuthAdapter(
$dbAdapter,
'users',
'username',
'password'
);
$authAdapter
->setIdentity($securePass->username)
->setCredential($securePass->password);
$result = $authAdapter->authenticate($authAdapter);
echo $result->getIdentity() . "\n\n";
}
else {
}
答案 0 :(得分:1)
Zend的做法是使用Authentication组件为你处理这个问题。
http://framework.zend.com/manual/current/en/modules/zend.authentication.intro.html
这将允许您检查用户是否已登录(您必须先设置身份验证适配器):
String temp = "[[0xFF008041, 0x24008086, 0x00000000, 0x00000000,0x0008383A], [0x0008, 0x0034B]]";
int[][] ConvertStringToJaggedArray(String input)
{
String[] Separator = { "],[", "], [", "] ,[" };
String[] OuterArray = input.Split(Separator, StringSplitOptions.RemoveEmptyEntries);
int[][] TargetArray = new int[OuterArray.Length][];
string HexString;
for (int i = 0; i < OuterArray.Length; i++)
{
String[] InnerArray = OuterArray[i].Split(',');
TargetArray[i] = new int[InnerArray.Length];
for (int j = 0; j < InnerArray.Length; j++ )
{
HexString = InnerArray[j].Trim(" []".ToCharArray());
TargetArray[i][j] = Convert.ToInt32(HexString, 16);
}
}
return TargetArray;
}
要访问帖子数据,您还应该利用框架而不是直接访问$ _POST。在您的控制器中:
use Zend\Authentication\AuthenticationService;
// TODO set-up authentication adapter
$auth = new AuthenticationService()
$identity = $auth->getIdentity();
这将指导您完成向您的应用添加身份验证层的整个过程:
https://zf2.readthedocs.org/en/latest/modules/zend.authentication.adapter.dbtable.html
答案 1 :(得分:1)
使用Zend提供的AuthenticationService,将自动设置PHP会话中的用户。
理解身份验证机制的一个好处是阅读和编码以及身份验证的介绍: http://framework.zend.com/manual/current/en/modules/zend.authentication.intro.html#adapters
在自定义AuthenticationAdapter中,“在会话中设置用户”或身份持久性,将通过返回\ Zend \ Authentication \ Result以及authenticate()方法中的身份验证结果和用户身份来完成。
$user = $this->userService->findByEmail($this->email);
if($user !== false) {
if($this->encryption->verify($this->password, $user->getPassword()) {
return new Result(Result::SUCCESS, $user);
}
return new Result(Result::FAILURE, null);
}
$this->userService being the UserService that leads to the UserMapper
(more about Services: http://framework.zend.com/manual/current/en/in-depth-guide/services-and-servicemanager.html)
$user being the User entity with the encrypted password stored
$this->encryption being your encryption method (Zend\Crypt\Password\Bcrypt for example)
$this->email being the email/username provided by the form
$this->password being the password provided by the form
Result being Zend\Authentication\Result
这是一种简单的方法。更详细的结果类型是:
/**
* General Failure
*/
const FAILURE = 0;
/**
* Failure due to identity not being found.
*/
const FAILURE_IDENTITY_NOT_FOUND = -1;
/**
* Failure due to identity being ambiguous.
*/
const FAILURE_IDENTITY_AMBIGUOUS = -2;
/**
* Failure due to invalid credential being supplied.
*/
const FAILURE_CREDENTIAL_INVALID = -3;
/**
* Failure due to uncategorized reasons.
*/
const FAILURE_UNCATEGORIZED = -4;
/**
* Authentication success.
*/
const SUCCESS = 1;
答案 2 :(得分:0)
LoginController.php
app.directive('validatorDirective', [
function ($timeout) {
return {
restrict: 'A',
require: 'ngModel',
link: function ($scope, $element, $attr, $controller) {
$scope.$watch($attr.ngModel, function (val) {
// no output when validator returns false, but returns model updates when validator returns true
console.log(val);
});
$controller.$validators.validFn = function (modelValue, viewValue) {
return false;
};
}
}
}
}]);