Gossamer Forum
Home : Products : DBMan : Customization :

Add a limited numbers of records

Quote Reply
Add a limited numbers of records
Bonjour

I would like to set up the database so that a user is limited to a specified number of records. They would then have Add, Modify rights but could only add records up to their limit.
Every user will have the same maximum number of records.

In db.cgi, sub validate record, I changed (as I read in a thread)

Code:
code:
if ($data[$db_key_pos] eq $in{$db_key}) {
return "duplicate key error";
}
}
close DB;

to
code:
Code:
if ($data[$db_key_pos] eq $in{$db_key}) {
return "duplicate key error";
}
if ($data[$auth_user_field] eq $db_userid) {
++$user_count;
}
}
close DB;
if ($user_count >= X) {
return "maximum number of records reached"
}

X= number of records allowed per user.

My problem is that the admin can only add X records too.
I'd like to setup the database so that a user is limited to add X records and the admin (in this case, me) is not limited.

Any ideas on how to do that would be appreciated.

Dominique
(learning english, perl, the way of dbman works by means of this forum).
Quote Reply
Re: Add a limited numbers of records In reply to
Try these codes:

Code:
unless (($per_admin) {
if ($data[$db_key_pos] eq $in{$db_key}) {
return "duplicate key error";
}
if ($data[$auth_user_field] eq $db_userid) {
++$user_count;
}
}
close DB;
if ($user_count >= X) {
return "maximum number of records reached"
}
}

# X= number of records allowed per user.

Basically, the only codes I added are the following:

Code:
unless (($per_admin) {

with the closing right bracket at the end of the codes.

Hope this works and helps.

Regards,



------------------
Eliot Lee
Founder and Editor
Anthro TECH, L.L.C
http://www.anthrotech.com/
info@anthrotech.com
==========================
Coconino Community College
http://www.coco.cc.az.us/
Web Technology
Coordinator
elee@coco.cc.az.us
Quote Reply
Re: Add a limited numbers of records In reply to
Bonjour

Thanks very much. Your quick answer helps me a lot.


In db.cgi, sub validate record
Code:
if ($data[$db_key_pos] eq $in{$db_key}) {
return "duplicate key error";
}
unless ($per_admin) {
if ($data[$auth_user_field] eq $db_userid) {
++$user_count;
}
}
}

close DB;
if ($user_count >= 2) {
return "<br>Vous .... etc"
}
Regards,

Dominique(learning english, perl, the way of dbman works by means of this forum).
Quote Reply
Re: Add a limited numbers of records In reply to
Is it possible to limit the number of records on a specific day?

I would like to only allow 5 records per person per day.

Thanks!
Adam
Quote Reply
Re: Add a limited numbers of records In reply to
You're getting really complicated here!

Let's see.

Code:
unless (($per_admin) {
if ($data[$db_key_pos] eq $in{$db_key}) {
return "duplicate key error";
}
if ($data[$auth_user_field] eq $db_userid) {
if ($data[number of your date field] eq &get_date) {
++$user_count;
}
}
}
close DB;
if ($user_count >= X) {
return "maximum number of records per day reached"
}
}

# X= number of records allowed per user.

I think I got all the brackets right there. It gets kinda confusing sometimes.




------------------
JPD








[This message has been edited by JPDeni (edited April 14, 2000).]
Quote Reply
Re: Add a limited numbers of records In reply to
Thanks I'll give it a try and let you know.
Quote Reply
Re: Add a limited numbers of records In reply to
This is nice, but doesn't this perform the count check after they hit the submit button ? Any way to do it before they try to add the record?
Quote Reply
Re: Add a limited numbers of records In reply to
What? You want it to make your coffee in the morning, too?!?!?! Smile

I suppose you could do a check when they go to the add form.

Code:
open (DB, "<$db_file_name") or &cgierr("error in validate_record.
unable to open db file: $db_file_name.\nReason: $!");
if ($db_use_flock) { flock(DB, 1); }

LINE: while (<DB> ) {
(/^#/) and next LINE;
(/^\s*$/) and next LINE;
$line = $_; chomp ($line);
@data = &split_decode($line);
if ($data[$auth_user_field] eq $db_userid) {
++$user_count;
}
}
close DB;

if ($user_count >= X) {
$message = "maximum number of records reached";
}

Then, instead of

Code:
&html_record_form (&get_defaults);

use

Code:
if ($message) {
print $message;
}
else {
&html_record_form (&get_defaults);
}

None of this is tested, of course, but it seems like it should work.

In anticipation of the next permutation of limiting users to a certain number of records, you could even prevent the "Add" link from printing out in the footer. Of course, it would mean that, as the user went to any page in the database, a full search of the database would be performed. Still, if you wanted to do that, you could add the following to sub html_footer:

Code:
open (DB, "<$db_file_name") or &cgierr("error in validate_record.
unable to open db file: $db_file_name.\nReason: $!");
if ($db_use_flock) { flock(DB, 1); }

LINE: while (<DB> ) {
(/^#/) and next LINE;
(/^\s*$/) and next LINE;
$line = $_; chomp ($line);
@data = &split_decode($line);
if ($data[$auth_user_field] eq $db_userid) {
++$user_count;
}
}
close DB;

if ($user_count >= X) {
$per_add = 0;
}

You probably would need to add the code in all three places -- sub html_footer, sub html_add_form and sub validate_record -- just in case someone tried to subvert the process.

Oh, and be sure you change the X to match the number of records a user is allowed.




------------------
JPD






Quote Reply
Re: Add a limited numbers of records In reply to
I try to use this pice of code but i always get a number 32 for the $auth_user_field
and this is never equal with $db_userid

any idea where the number 32 is coming from ?
when i login as admin i get 32 or any other name

Thanks anyway
Quote Reply
Re: Add a limited numbers of records In reply to
Which code did you add? (There are several different bits of code here.) Did it work correctly before you added the code?

It sounds to me like there's something in your .cfg file that isn't correct, but I'm not sure what it is.

Please copy your .cfg file to a web-accessible directory (one where you would place .html files) and rename it to default_cfg.txt. Then come back and tell me where I can find it and I'll take a look.


------------------
JPD






Quote Reply
Re: Add a limited numbers of records In reply to
The default_cfg.txt is at
http://208.230.131.192/default_cfg.txt

Ther are many topics about limited record what's the best you think.
Quote Reply
Re: Add a limited numbers of records In reply to
Sorry nearly every code I tryed
Quote Reply
Re: Add a limited numbers of records In reply to
It's working

I didn't understand to put the code in the db.cgi file but in the html.pl file.

Thanks for your support.