Official Support Area, Q&As, Discussions, Suggestions and Bug reports.
Forum's Guidelines
May I know your license number? It seems you posted from unlicensed one. Plus our SLA for licensed users is 24 hour response and we will assist you within 24h. Keep spamming is not a good way to get attention. Here is our forum guidelines.
And please tell me exactly what you want to complete in dashboard?
Yes, of course, you can use Dashboard for multiusers and create dashboard based on a specific logged user. The user A should see data of his, and user B should see B's data. In this post we have answer how to use authentication with SQL DataSource to facilitate user login.
So when a user login, you will get a User object containing id, name and role of them that you can access anywhere from $this->app()->user()
, for example in a Widget, you can get data for specific user like this:
class PaymentTable extends Table
{
protected function dataSource()
{
$userId = $this->app()->user()->id();
return AutoMaker::table("payments")->where("user_id",$userId);
}
}
Hope that helps.
Well, you just need to define a datasource class like this:
AutoMaker here is an datasource extending from MySQL. And inside connection() method, you define your connection to your database.
After that, you can use your datasource anywhere in your application. for example:
AutoMaker::table("customers")->run(); //Return all customers
More about datasource, you can read our documentation here
Let us know if you need further assistance.
App.php
use \koolreport\dashboard\Application;
use \koolreport\dashboard\pages\Login;
use \koolreport\dashboard\User;
class App extends Application
{
protected function login()
{
return Login::create()
->authenticate(function($username,$password) {
$users = User::table("users")
->where("username",$username)
->where("password",md5($password))
->run();
// The $users is DataStore object which contain list of user,
// something like Collection class in Laravel if you know.
$user = $users->get(0);
if($user==null) return null;
return User::create()
->id($user["id"])
->name($user["name"]);
});
}
}
I see that you use the User class in here
$users = User::table("users") // <--here
->where("username",$username)
->where("password",md5($password))
->run();
which seems not correct. It should be your datasource class, for example like AutoMaker in our example.
May you post also your datasource class.
Please remember: Our code is just example, you need to apply to your own case. For example, we use column "username" and "password" but they may have other names in your database.
<?php //App.php use \koolreport\dashboard\Application; use \koolreport\dashboard\pages\Login; use \koolreport\dashboard\User;
class App extends Application {
protected function login()
{
return Login::create()
->authenticate(function($username,$password) {
$users = AutoMaker::table("users")
->where("username",$username)
->where("password",md5($password))
->run();
// The $users is DataStore object which contain list of user,
// something like Collection class in Laravel if you know.
$user = $users->get(0);
if($user==null) return null;
return User::create()
->id($user["id"])
->name($user["name"]);
});
}
} don't work
Okay. Does your database "prueba" have table name called "users"? Does the table "users" has column "username" and "password"? I guess there is not, so you will get error. What I shown you here is just pseudo code to guide you how to access database to authenticate user. You need to replace or write your own query. Don't just copy because surely it will not work.
Another suggestion: Instead of using AutoMaker as the datasource name, you should use name like Prueba. It will make more sense.
<?php
//Prueba.php
use \koolreport\dashboard\sources\MySQL;
class Prueba extends MySQL
{
protected function connection()
{
return [
"connectionString"=>"mysql:host=localhost;dbname=prueba",
"username"=>"User",
"password"=>"sdfsdfsdf",
"charset"=>"utf8"
];
}
}
then later you can query like this:
Prueba::table("your-table-name")->select("column-name")->run();
Please tell me what does not work? What error do you get? "Does't work at all" can not help me to assist you.
Questions:
In general, the authenticate() function will get you the $username and $password which will need you to verify. If username and password is not correct, you return null
which will show Something wrong ...
result. On contrary, if you find the user with username and password from database, you will return User object with user information like id or name. The logic is simple as that.
THIS IS THE PROBLEM, WHEN I TRY WITH
return Login::create()
->authenticate(function($username,$password) {
$users = AutoMaker::table("users")
->where("username",$username)
->where("password",md5($password))
->run();
// The $users is DataStore object which contain list of user,
// something like Collection class in Laravel if you know.
$user = $users->get(0);
if($user==null) return null;
return User::create()
->id($user["id"])
->name($user["name"]);
});
}
} ____THIS IT WORKS
return Login::create()
->authenticate(function($username,$password){
if($username==="admin@example.com" && $password==="admin_password")
{
// If authenticated, return User object
return User::create()
->id(1)
->name("Admin")
->avatar("avatars/8.jpg")
->roles(["admin"]);
}
// If unauthenticated, return null
return null;
});
}
}
Let try this:
return Login::create()
->authenticate(function($username,$password) {
$users = AutoMaker::table("users")
->where("username",$username)
->where("password",$password)
->run();
// The $users is DataStore object which contain list of user,
// something like Collection class in Laravel if you know.
$user = $users->get(0);
if($user==null) return null;
return User::create()
->id($user["id"])
->name($user["name"]);
});
}
Make sure you have "users" table and in table you have id
,username
,password
and name
columns. Let try to have a row like this:
id | username | password | name |
---|---|---|---|
1 | admin@example.com | admin_password | Admin |
the problem is in the code you give me
return Login::create()
->authenticate(function($username,$password) {
$users = AutoMaker::table("users")
->where("username",$username)
->where("password",$password)
->run();
// The $users is DataStore object which contain list of user,
// something like Collection class in Laravel if you know.
$user = $users->get(0);
if($user==null) return null;
return User::create()
->id($user["id"])
->name($user["name"]);
});
}
Well, it always take a little time for learning, be patient and you will make progress, no worry. Thousand of developers have used Dashboard framework so for sure we will not make a not-working product and we can sell.. so no worry, you will get what you need. I have talked to dev.team to make a simple working example for you and will share code to Github. I will keep you update on this.
Our dev.team has been added "login using database" into our demo. Here is the working code
protected function login()
{
return Login::create()
->descriptionText("
<i style='color:#333'>
Please log in with <b class='text-danger'>demo</b>/<b class='text-danger'>demo</b>
</i>
")
->failedText("Wrong! Please use <b>demo</b> for both username and password!")
->authenticate(function ($username, $password) {
//Look for user that have username and password
$users = AutoMaker::table("users")
->where("username",$username)
->where("password",$password)
->run();
$user = $users->get(0);//Try to get first user, get associate array contain user information
if($user!==null) {
//Found a user, return User object
return User::create()
->id($user["id"])
->name($user["displayname"])
->avatar("images/8.jpg")
->roles([$user["role"]]);
}
//Other: fail to login, return null
return null;
});
}
Alternatively you can View On Github.
We have added a table called "users" to AutoMaker database and here is the schema and data of "users" table:
--
-- Table structure for table `users`
--
CREATE TABLE `users` (
`id` int(11) NOT NULL,
`username` varchar(50) NOT NULL,
`password` varchar(50) NOT NULL,
`displayname` varchar(50) NOT NULL,
`role` varchar(10) NOT NULL
) ENGINE=InnoDB DEFAULT CHARSET=latin1;
--
-- Dumping data for table `users`
--
INSERT INTO `users` (`id`, `username`, `password`, `displayname`, `role`) VALUES
(1, 'admin', 'admin', 'Admin', 'admin'),
(2, 'demo', 'demo', 'Demo', 'user');
--
-- Indexes for dumped tables
--
--
-- Indexes for table `users`
--
ALTER TABLE `users`
ADD PRIMARY KEY (`id`);
--
-- AUTO_INCREMENT for dumped tables
--
--
-- AUTO_INCREMENT for table `users`
--
ALTER TABLE `users`
MODIFY `id` int(11) NOT NULL AUTO_INCREMENT, AUTO_INCREMENT=3;
We have implemented the login into our online demo as well at Dashboard Demo. So according to data of users table, beside login with "demo/demo", you can login with "admin/admin".
Hope that helps.
This is just example to show you how to do login with user data from database. We want to show you the frame of code, how it should be done and also show you that it works. Your application will have its own unique characteristics like different tables, different columns so you will need to insert your own code that we will not able to write for you, you need to do it by yourself.
The framework like Dashboard will save you effort in long run but not in short run since you will have to take little time to learn structure of framework. It is like you purchase an microwave to cook food faster but still you need to learn how to use microwave.
About the role of user, you insert list of roles into roles()
method (the list o role can comes from db), for example:
User::create()
->roles(["user","moderator"]);
So later in any where, you can do:
if($this->app()->user()->hasRole("moderator")) {
//Doing something
}
That's how user roles works.
APP.PHP
<?php
//App.php
use \koolreport\dashboard\menu\Section;
use \koolreport\dashboard\menu\Group;
use \koolreport\dashboard\pages\Login;
use \koolreport\dashboard\User;
class App extends \koolreport\dashboard\Application
{
protected function login()
{
return Login::create()
->failedText("Wrong! Please use <b>demo</b> for both username and password!")
->authenticate(function ($username, $password) {
//Look for user that have username and password
$users = ConnectDB::table("users")
->where("username",$username)
->where("password",$password)
->run();
$user = $users->get(0);//Try to get first user, get associate array contain user information
if($user!==null) {
//Found a user, return User object
return User::create()
->id($user["id"])
->name($user["displayname"])
->avatar("images/8.jpg")
->roles([$user["role"]]);
}
//Other: fail to login, return null
return null;
});
}
}
Now the real error is showing not like "Something wrong", it is not working because your php could not connect to MySQL due to "could not find driver".
You need to have a driver module called pdo_mysql
. Please install that module to your php. Below link might help:
Alternatively, you can use XAMPP which has everything installed for you.
Hope that helps.
Let KoolReport help you to make great reports. It's free & open-source released under MIT license.
Download KoolReport View demo