Files
dojo/lib/Dojo/Model/Users.pm

96 lines
2.4 KiB
Perl
Raw Permalink Normal View History

2018-07-13 19:06:08 -05:00
package Dojo::Model::Users;
2018-07-17 18:53:49 -05:00
2018-07-13 19:06:08 -05:00
use strict;
use warnings;
2019-04-04 18:04:54 -06:00
use Mojo::File 'path';
2018-07-13 19:06:08 -05:00
use DBI;
use Mojo::Util 'secure_compare';
use Dojo::Conf;
2019-04-04 18:04:54 -06:00
my $data_path = path('lib/Dojo/Model/Data')->make_path;
2018-07-13 19:06:08 -05:00
sub new { bless {}, shift }
2019-03-19 02:56:18 -06:00
sub createu{
my ($c,$user,$uname,$tel,$mail,$pass,$passchk)=@_;
my $ttel= $tel =~m/^$/ ? "0000000000" : $tel;
$ttel =~ s/[^\+0-9]//g;
if(
$ttel =~m/\+?[0-9]{10,}/ &&
Email::Valid->address($mail) &&
$user =~m/[a-zA-Z]{3,25}/ &&
$user !~m/\s/ && #mejora este
$uname =~m/[a-zA-Z]{3,50}/ &&
($pass eq $passchk)
){
if(
$user =~m/\s|sex|http|webpage/ &&
$uname =~m/sex|http|webpage/
){ return 2} #possible spam
# $ttel = $tel =~m/^$/? "null";
if ( _write(
"insert into usuario (usuario,nombre,telefono,correo,pass,permiso_id,ultima)
values (?,?,?,?,?,2,now())",
$user,$uname,$ttel,$mail,$pass) == 0){return 0;}
return 1;
2018-07-27 17:41:31 -05:00
}
2019-03-19 02:56:18 -06:00
return 0;
}
sub grulla_pass{
return
_read( "select pass as gpass from usuario where usuario like 'grullas'")->[0]
};
sub ugrulla_pass{
my($c,$p)=@_;
_write("update usuario set pass= ? where usuario like 'grullas'",$p);
}
2018-07-27 17:41:31 -05:00
2018-07-13 19:06:08 -05:00
sub check {
my ($self, $user, $pass) = @_;
2019-03-19 02:56:18 -06:00
my $q="select permiso_id as pid from usuario where usuario = ? and pass=? ";
2018-07-13 19:06:08 -05:00
# | 0 | bloqueado |
# | 1 | invitado |
# | 2 | usuario |
# | 3 | pago |
# | 4 | master |
2018-07-17 18:53:49 -05:00
my $r= _read($q,$user,$pass)->[0];
2018-07-13 19:06:08 -05:00
return $r->{pid} // 0;
}
2019-04-04 18:04:54 -06:00
sub store{
return _read ($data_path->child("/store/qStore.q")->slurp);
}
2019-04-04 19:45:51 -06:00
sub store_id{
my ($self,$id)=@_;
return _read ($data_path->child("/store/qIdStore.q")->slurp,$id)->[0];
}
2018-07-13 19:06:08 -05:00
sub _read{
2018-07-17 18:53:49 -05:00
my ($q,@bind)=@_;
2018-07-13 19:06:08 -05:00
my (@empty,$arr);
2018-08-15 02:47:30 -05:00
my $dbh = DBI->connect("DBI:mysql:".Dojo::Conf::GRULLADB.":".Dojo::Conf::GRULLADB_H,Dojo::Conf::GRULLADB_UR,Dojo::Conf::GRULLADB_URP,{mysql_enable_utf8 => 1});
2018-07-13 19:06:08 -05:00
return \@empty unless($dbh);
#$dbh->do(qq{SET NAMES 'utf8'});
2018-08-16 03:18:46 +00:00
my $h=$dbh->selectall_arrayref($q,{ Slice => {} },@bind)//"error";
2018-07-13 19:06:08 -05:00
#((col1=>d1,col2=>d1),(col1=>d2,col2=>d2))
$dbh->disconnect();
return $h;
}
2018-07-27 17:41:31 -05:00
sub _write{
my ($q,@bind)=@_;
my (@empty);
2018-08-16 03:18:46 +00:00
my $dbh = DBI->connect("DBI:mysql:".Dojo::Conf::GRULLADB.":".Dojo::Conf::GRULLADB_H,Dojo::Conf::GRULLADB_UW,Dojo::Conf::GRULLADB_UWP,{mysql_enable_utf8 => 1});
2018-07-27 17:41:31 -05:00
return 0 unless($dbh);
2018-08-16 03:18:46 +00:00
my $h=$dbh->do($q,{ Slice => {} },@bind)//"error";
2018-07-27 17:41:31 -05:00
$dbh->disconnect();
# log("db write:". $h );
return $h;
}
2018-07-13 19:06:08 -05:00
1;