Recent

Author Topic: How do I filter the users?  (Read 4266 times)

workinghard

  • Jr. Member
  • **
  • Posts: 74
How do I filter the users?
« on: March 15, 2012, 10:12:27 am »
I need to be able to filter the users in my program. There are some tables for basic identity, bills, and registration numbers. These users (the normal users) are granted permission to use all those tables, but i don't really know how to filter the records so that it links to specifically that user
what should i do?

JD

  • Hero Member
  • *****
  • Posts: 1916
Re: How do I filter the users?
« Reply #1 on: March 15, 2012, 01:34:09 pm »
There are two options to consider:

a) The filter is for the client program which displays the data

In this case, the appropriate use of queries or even stored procedures can limit what the user sees/edits to his access level.

b) The objective is to give users permission to view/edit tables on the database end

In this case, you better look at the documentation of the database you are using and get acquainted with SQL commands like GRANT. You can read this brief tutorial for more information http://databases.about.com/od/security/a/accesscontrols_2.htm.
« Last Edit: March 15, 2012, 01:36:10 pm by JD »
Linux Mint - Lazarus 4.8/FPC 3.2.2,
Windows - Lazarus 4.8/FPC 3.2.2

mORMot 2, PostgreSQL & MariaDB.

workinghard

  • Jr. Member
  • **
  • Posts: 74
Re: How do I filter the users?
« Reply #2 on: March 18, 2012, 10:23:04 am »
About the first option, how do i do it?
There are many user in my table and i need to filter it to specifically their data

So, if, for example if steve logs in to his mysql database account, in the memo pad, it will display steve's recent payment and date, not others
How can i do it?

BigChimp

  • Hero Member
  • *****
  • Posts: 5740
  • Add to the wiki - it's free ;)
    • FPCUp, PaperTiger scanning and other open source projects
Re: How do I filter the users?
« Reply #3 on: March 18, 2012, 12:50:21 pm »
About the first option, how do i do it?
There are many user in my table and i need to filter it to specifically their data
The appropriate use of queries, as JD mentioned.  Something like (don't know if this is valid SQL, let alone MySQL SQL):
Code: [Select]
// assuming you have a Bills table with ID, Year, Month, Total, Description, User fields....
// see note below on joins
SELECT ID, Year, Month, Total, Description FROM Bills WHERE User = 'steve';

Depending on MySQL functionality, you could put this into a stored procedure... or used a parametrised query
http://wiki.lazarus.freepascal.org/Working_With_TSQLQuery#Parameters_in_TSQLQuery.SQL
Something like: (air code as well, does probably not compile):
Code: [Select]
StatementQuery.SQL.Text:='SELECT Year, Month, Total, Description FROM Bills WHERE User = :TheUserIWant;';
StatementQuery..params.parambyname('TheUserIWant').asstring := 'steve';

So, if, for example if steve logs in to his mysql database account, in the memo pad, it will display steve's recent payment and date, not others
How can i do it?
See above; it might be a good idea to link your records to a mysql user id, something like a table ElectricityUsers with fields ID, MySQLUserName.
Make the ID an autonumber/autoincrement field.
Now you get e.g.
Code: [Select]
ID MySQLUserName
2  Bill
3  Steve
18 John
... in this table you can also add his real name, birthdate, and other relevant info about that person.

Then you can store the numeric userid in all your tables instead of the full name:
eg
Bills table with ID, Year, Month, Total, Description, User fields....
becomes
Bills table with ID, Year, Month, Total, Description, UserID fields. UserID is the number for Steve

When a user logs in, you know his username. You can retrieve his ElectricityUsers.ID value and use that in all your queries.

I suggest you first try and get some database/Lazarus experience by going through the tutorials, e.g. http://wiki.lazarus.freepascal.org/SQLdb_Tutorial1 on the wiki page, if you haven't already.

Good luck,
BigChimp
Want quicker answers to your questions? Read http://wiki.lazarus.freepascal.org/Lazarus_Faq#What_is_the_correct_way_to_ask_questions_in_the_forum.3F

Open source including papertiger OCR/PDF scanning:
https://bitbucket.org/reiniero

Lazarus trunk+FPC trunk x86, Windows x64 unless otherwise specified

workinghard

  • Jr. Member
  • **
  • Posts: 74
Re: How do I filter the users?
« Reply #4 on: March 18, 2012, 03:19:22 pm »
how do i link up the mysqluser with the incremental id number then?

should i make a new table? but that will defeat the purpose of doing it using mysql login right?
« Last Edit: March 18, 2012, 03:30:29 pm by workinghard »

 

TinyPortal © 2005-2018