After switch to mongoDB auth::attempt does not work anymore - mongodb

I recently switched my Laravel 4 Project to the mongoDB System using the jenssegers/laravel-mongodb Package.
The Seeds and migrations are fine, but the Auth::attempt function always returns false now. This worked quite nice before.
This is how the attribute looks like in the seeds:
'password' => Hash::make ( 'password' ),
This is how the data is compared by Auth::attempt:
$userdata = array(
'email' =>Input::get('email'),
'password' => Input::get('password'),
);
$loginResult = Auth::attempt($userdata, $post_remember);
I inserted
use Jenssegers\Mongodb\Model as Eloquent;
in all Models, Seeds, Migrations and the controller.
But auth.attempt ALWAYS returns false.

Since you are using Laravel 4 you can fix this by implementing "UserInterface" in your User model.
use Illuminate\Auth\UserInterface;
use Jenssegers\Mongodb\Model as Eloquent;
and then:
class User extends Eloquent implements UserInterface
{
}
For Laravel 5:
use Illuminate\Auth\Authenticatable;
use Illuminate\Contracts\Auth\Authenticatable as AuthenticatableContract;
use Jenssegers\Mongodb\Model as Eloquent;
class User extends Eloquent implements AuthenticatableContract
{
use Authenticatable;
}

Related

SoftDeletes Memory issue with php 7.4 and Illuminate 7.9.2

We have a project with eloquent (without Laravel).
Recently we upgrade to php 7.4, and then Iluminate/database to 7.9.2
In seeder, when we insert data into model without SofDelete, it works.
But when model have softdelete, it hangs.
Example Role:
use Illuminate\Database\Eloquent\Model as Eloquent;
class Role extends Eloquent {
protected $table = 'usuarios_roles';
public $timestamps = true;
...
$admin = Role::create(array(
'nombre' => 'Admin',
'vista' => '1',
'path' => '/admin/clientes/',
'editables' => 0
));
Works fine.
Example User:
require_once('EloquentRegAcc.php');
use Illuminate\Database\Eloquent\SoftDeletes;
class Usuario extends EloquentRegAcc
{
use SoftDeletes;
public $timestamps = true;
protected $dates = ['deleted_at'];
protected $table = 'usuarios';
...
$usuario_admin = Usuario::create(array(
'id_role' => $admin2->id,
'nombre' => 'Admin',
'email' => $email,
'password' => $password2,
'permissions' => '{}'
));
It give us an error:
PHP Fatal error: Allowed memory size of 4294967296 bytes exhausted (tried to allocate 20480 bytes) in /home/vagrant/code/project/vendor/illuminate/database/Eloquent/SoftDeletes.php on line 36
We try to upgrade Vagrant RAM, from 2GB to 4GB, and set ini_set('memory_limit', '4096M') , but no way.
In the database we have roles, but not users.
Same code with php 7.1 and Iluminate/database 5.x works fine.
not matter if is user or other table; if model have SoftDelete, hangs.
We have a "homemade" log in the app.
If we change
require_once('EloquentRegAcc.php');
use Illuminate\Database\Eloquent\SoftDeletes;
class Usuario extends EloquentRegAcc
{
To
use Illuminate\Database\Eloquent\Model as Eloquent;
use Illuminate\Database\Eloquent\SoftDeletes;
class Usuario extends Eloquent
{
Works as spect. We need to rewrite or log.

Loses the connection name when obtaining a record (Outside Laravel / enssegers/laravel-mongodb)

I need your help, I am using Eloquent and enssegers / laravel-mongodb outside Laravel Framework, I have managed to correctly configure both Eloquent and laravel-mongodb and it works correctly to insert and obtain results, however, when trying to modify a registry, eloquent loses the connection name by throwing the following error:
Argument 1 passed to Jenssegers\Mongodb\Query\Builder::__construct() must be an instance of Jenssegers\Mongodb\Connection, instance of Illuminate\Database\MySqlConnection given, called in /www/html/syberianbox/sachiel/vendor/jenssegers/mongodb/src/Jenssegers/Mongodb/Eloquent/Model.php on line 421
I share the configuration of Eloquent through Capsula and laravel-mongodb
$this->_capsule = new Capsule();
$this->_capsule->getDatabaseManager()->extend('mongodb', function($config) {
return new MongodbConnect($config);
});
//MySQL connection, $ this -> _ config contains an array with the correct values
$this->addConnection('default', [
'driver' => $this->_config['default']['driver']
,'host' => $this->_config['default']['host']
,'database' => $this->_config['default']['database']
,'username' => $this->_config['default']['username']
,'password' => $this->_config['default']['password']
,'charset' => $this->_config['default']['charset']
,'collation' => $this->_config['default']['collation']
,'prefix' => $this->_config['default']['prefix']
]);
//MongoDB connection
$this->addConnection('archivos', [
'driver' => $this->_config['archivos']['driver']
,'host' => $this->_config['archivos']['host']
,'port' => $this->_config['archivos']['port']
,'database' => $this->_config['archivos']['database']
]);
Model:
<?php
namespace Instances\Generic\Models\CFDI;
use Jenssegers\Mongodb\Eloquent\Model;
class Archivo extends Model
{
protected $collection = 'cfdi';
protected $connection = 'archivos';
}
Execute model:
$archivoDB = Archvio::Where('_id',$id)->first();
$this->_logger->info('Archivo: ', $archivoDB);
$archivoDB->uuid = $uuid;
$archivoDB->type = $type;
$archivoDB->content = $content;
$archivoDB->save();
Logger:
[2019-02-12 14:20:36:511603][Instances/Generic/Modules/Administracion/ArchivosController.php : 75][Info][30117] Archivo:
Instances\Generic\Models\CFDI\Archivo Object
(
[collection:protected] => cfdi
[connection:protected] =>
[primaryKey:protected] => _id
[keyType:protected] => string
.....
I was able to solve the problem, however it is more a patch than a complete solution.
I extend the class Model of jenssegers / laravel-mongodb and on I write the method getConnectionName () and return the name of the expected connection, as in my case it is only a single connection the name remains as static, however, the method gives the arrow to be able to include all the necessary logic to identify the connection with the model in question
Model:
<?php
namespace Syberianbox\Db;
use Jenssegers\Mongodb\Eloquent\Model;
class Monomodel extends Model
{
public function getConnectionName() {
return 'archivos';
}
}

CakePHP - Using a different model in current model

I am creating a custom validation function in my model in CakePHP. After reading similar questions I have understood that I could be using ClassRegistry::init('Model'); to load a foreign model in my current model. But it doesn't say much more on the syntax and how to actually use it afterwards. This is what I have tried, but nothing "is happening" when I am trying to print the array to see if it contains the right stuff. Basically I want to pull out the User data to use it in my validation.
class Booking extends AppModel {
public $name = 'Booking';
public $validate = array(
'start_time' => array(
'noOptionViolation' => array(
'rule' => 'noOptionViolation',
'allowEmpty' => false
)
),
);
public function noOptionViolation ($start_time) {
$this->User = ClassRegistry::init('User');
$allUsers = $this->User->find('all');
print_r($allUsers);
}
Is this correct syntax? Can I use all the methods of $this->User just like I would in a controller?
You can use import as detailed on this post:
https://stackoverflow.com/a/13140816/1081396
App::import('Model', 'SystemSettings.SystemSetting');
$settings = new SystemSetting();
$mySettings = $settings->getSettings();
In your example it would be like:
App::import('Model', 'Users.User');
$user = new User();
$allUsers = $user->find('all');
print_r($allUsers);
You could better use the import at the beginning of the model.
You could use this too to load Models
$this->loadModel('User');
and access all functions by
$this->User

Zend Sessions getting mixed up

I'm currently working on the login for a Zend Framework application, and I'm using a combination of Zend_Auth and Zend_Session using a database adapter (as described in the Zend Framework manuals).
I've made a resource for the session:
class DC_Resource_DbSession extends Zend_Application_Resource_ResourceAbstract{
public function init(){
}
public function setadapter($value){
$this->dbAdapter = $value;
}
public function setSession($adapter){
//put your code here
$config = array(
'name' => 'sessions',
'primary' => 'id',
'modifiedColumn' => 'modified',
'dataColumn' => 'data',
'lifetimeColumn' => 'lifetime',
'db' => $adapter
);
Zend_Session::setSaveHandler(new Zend_Session_SaveHandler_DbTable($config));
Zend_Session::setOptions(array('name'=>'MY_SESSION_NAME'));
Zend_Session::start();
}
}
Zend Auth then uses the session to store some rudimentary authentication information.
However, when testing the login (from the same IP) if one user in our office logs in and another user goes to the site, they are logged in as the user!!! Can anyone help me figure out why they are using each other's sessions?
DOH!!!!
My humble, humiliated apologies. The id column datatype had been set to INT by our migrations guy - and obviously it needs to be VARCHAR....
smacks self in face...

Using Zend Framework Db Tables without MVC

I am trying to use the Zend Framework without using the MVC structure, specifically the Db_Table classes.
I have created a couple of classes representing my database tables, i.e.
class DBTables_Templates extends Zend_Db_Table_Abstract
{
protected $_name = "templates";
}
When I try to instantiate this class (it is included fine), I get the following error:
Fatal error: Uncaught exception 'Zend_Db_Table_Exception' with message 'No adapter found for DBTables_Templates'
Does anyone know how I create and include the database adapter for the Db_Table classes to use?
Any pointers are greatly appreciated! I am using the latest version of ZF.
You need to create a Zend_Db_Adapter, which is the class you use to connect to the database.
$db = new Zend_Db_Adapter_Pdo_Mysql(array(
'host' => '127.0.0.1',
'username' => 'webuser',
'password' => 'xxxxxxxx',
'dbname' => 'test'
));
Or you can use the factory() method to make instantiation more configurable:
$db = Zend_Db::factory('Pdo_Mysql', array(
'host' => '127.0.0.1',
'username' => 'webuser',
'password' => 'xxxxxxxx',
'dbname' => 'test'
));
See http://framework.zend.com/manual/en/zend.db.html#zend.db.adapter.connecting
Then specify this adapter object to your table class. There are at least three ways to do this:
Set an application-wide default for all tables:
Zend_Db_Table_Abstract::setDefaultAdapter($db);
Specify the adapter to the table constructor:
$table = new MyTable( array('db'=>$db) );
Store the adapter in the registry and specify it to the table or set it as default:
Zend_Registry::set('my_db', $db);
$table = new MyTable( array('db'=>'my_db') );
// alternatively:
Zend_Db_Table_Abstract::setDefaultAdapter('my_db');
See http://framework.zend.com/manual/en/zend.db.table.html#zend.db.table.constructing