473,503 Members | 1,818 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Synchronising multiple common fields among tables

Hi,

(I apologise if this is the wrong list, I haven't posted
to a postgresql.org mailing list before, general seemed
like a good catch-all)

I am trying to implement a centralised authentication system
using PostgreSQL as the central repository for user and password
information. I want to keep a table which contains core user
details like the username and password and then use this data
from a number of other tables which will use the username and
password as well as adding their own service-specific fields.

My Plan:
Each service will have it's own table which to which
it has full priviledges. It can update the password and
other information which is linked from the central
username and password table and this will cascade back
up so that the users password for all the services is
changed. For Example, Samba is going to be accessing
this database via PAM so I would like it to be possible
for users to change their password from their Windows PC
and have their password for all their other services
updated at the same time. I don't want the services
to have any direct access to the central users table.

Example Tables:

core=# \d users
Table "public.users"
Column | Type | Modifiers
----------+-----------------------+-----------
uid | integer | not null
gid | integer | not null
username | character varying(32) | not null
password | character varying(64) | not null
fullname | character varying(64) | not null
Indexes: users_pkey primary key btree (uid)

core=# \d samba_users
Table "public.samba_users
Column | Type | Modifiers
-------------+-----------------------------+-----------
uid | integer | not null
gid | integer | not null
username | character varying(32) | not null
password | character varying(64) | not null
fullname | character varying(64) | not null
date | timestamp without time zone | not null
expired | integer | not null
newpassword | integer | not null
Indexes: samba_users_pkey primary key btree (uid)

core=# \d postfix_users
Table "public.postfix_users"
Column | Type | Modifiers
----------+------------------------+-----------
uid | integer | not null
gid | integer | not null
username | character varying(32) | not null
password | character varying(64) | not null
fullname | character varying(64) | not null
homedir | character varying(128) | not null
maildir | character varying(128) | not null
Indexes: postfix_users_pkey primary key btree (uid)

As these example tables hopefully illustrate the uid, gid,
username, password, and fullname fields should be linked
among all of the tables - but independantly accessed via
a service specific table.

Is this is a stupid plan ?

I had a number of ideas about how to implement this, one was
using a separate VIEW for each services table. However I
have discovered that PostgreSQL only supports read-only views
at the current time.

I also thought that I would be able to use a foreign key to
link the records together. I would like to link the records
on their uid and then have updates to the linked fields
cascaded back up to the main user table. I haven't been able
to figure out how to do this without making the password
field UNIQUE, which won't work very well.

My final idea was that I could use a TRIGGER as a method to
update the linked fields, I think I will be able to do this
if I create two functions for each service table which then
uses an UPDATE to synchronise the tables. This to me though
seems to be the ugliest way of doing what I want.

Is this my only option ?
Am I being stupid in general ?

NOTE: If you haven't guessed already a lot of this is new to
me, but I'll do my best to work with whatever advice I'm
given.

Tips, comments, suggestions all appreciated.

Thanks,
--
Dominic
<dom at cus.org.uk>

---------------------------(end of broadcast)---------------------------
TIP 6: Have you searched our list archives?

http://archives.postgresql.org

Nov 11 '05 #1
1 2570
do*@cus.org.uk (Dominic Marks) wrote in message news:<20********************@cus.org.uk>...
Hi, I had a number of ideas about how to implement this, one was
using a separate VIEW for each services table. However I
have discovered that PostgreSQL only supports read-only views
at the current time.
PostgreSQL does allow writable views - you must define a rule to
define the SQL to execute when an INSERT, UPDATE or DELETE is executed
against the view. See the PostgreSQL docs for CREATE RULE.

I also thought that I would be able to use a foreign key to
link the records together. I would like to link the records
on their uid and then have updates to the linked fields
cascaded back up to the main user table. I haven't been able
to figure out how to do this without making the password
field UNIQUE, which won't work very well.

My final idea was that I could use a TRIGGER as a method to
update the linked fields, I think I will be able to do this
if I create two functions for each service table which then
uses an UPDATE to synchronise the tables. This to me though
seems to be the ugliest way of doing what I want.

Is this my only option ?
Am I being stupid in general ?

NOTE: If you haven't guessed already a lot of this is new to
me, but I'll do my best to work with whatever advice I'm
given.

Tips, comments, suggestions all appreciated.


Try the following for an exmple of using rule on an view
CREATE TABLE users (
uid int PRIMARY KEY,
gid int NOT NULL,
username char(32) NOT NULL,
password char(32) NOT NULL,
fullname char(64) NOT NULL
);
CREATE TABLE samba_users_storage (
uid int NOT NULL PRIMARY KEY REFERENCES users ON DELETE CASCADE
ON UPDATE CASCADE,
date timestamp NOT NULL,
expired integer NOT NULL,
newpassword integer NOT NULL
);

CREATE OR REPLACE VIEW samba_users AS
SELECT u.*, sus.date, sus.expired, sus.newpassword FROM users u
INNER JOIN samba_users_storage sus USING (uid);

CREATE OR REPLACE RULE "INSERT" AS ON INSERT TO samba_users DO INSTEAD
(
INSERT INTO users (uid,gid,username,password,fullname) VALUES
(NEW.uid,NEW.gid,NEW.username,NEW.password,NEW.ful lname);
INSERT INTO samba_users_storage (uid,date,expired,newpassword)
VALUES (NEW.uid,NEW.date,NEW.expired,NEW.newpassword);
);

CREATE OR REPLACE RULE "UPDATE" AS ON UPDATE TO samba_users DO INSTEAD
(
UPDATE users SET username = NEW.username, password = NEW.password,
fullname = NEW.fullname WHERE uid = OLD.uid;
UPDATE samba_users_storage SET date = NEW.date, expired =
NEW.expired, newpassword = NEW.newpassword WHERE uid = OLD.uid;
);

CREATE OR REPLACE RULE "DELETE" AS ON DELETE TO samba_users DO INSTEAD
(
DELETE FROM samba_users_storage WHERE uid = OLD.uid;
);

CREATE TABLE postfix_users_storage (
uid int NOT NULL PRIMARY KEY REFERENCES users ON DELETE CASCADE
ON UPDATE CASCADE,
homedir char(128) NOT NULL,
maildir char(128) NOT NULL
);

INSERT INTO samba_users VALUES (1,1,'tom','secret','tom
hebbron',current_date,0,0);

SELECT * FROM users;
SELECT * FROM samba_users_storage;
SELECT * FROM samba_users;

Hope that helps - sorry I don't have time to complete the postfix
table - but use the samba view/rules as a template.

--
Tom Hebbron
Nov 11 '05 #2

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
2587
by: Beyonder | last post by:
I have five tables in my database, there are actually NO common fields between them, not even a KEY or ID or anything like that, except for the "body" of a blob field. and that text is not...
8
3294
by: Jan van Veldhuizen | last post by:
The UPDATE table FROM syntax is not supported by Oracle. I am looking for a syntax that is understood by both Oracle and SqlServer. Example: Table1: id name city ...
1
1408
by: Michael Thomas | last post by:
Hi everyone Having a slight problem synchronising tables in MS Access 2002. I have two tables: Products (StockCode*, Description, CostPrice, SellingPrice) and
0
1322
by: Chris Naylor | last post by:
Right, further to previous question not so long ago... I have a fairly complex access database that has a number of linked tables in it. The main table of this database is a details one that has...
22
23316
by: Matthew Louden | last post by:
I want to know why C# doesnt support multiple inheritance? But why we can inherit multiple interfaces instead? I know this is the rule, but I dont understand why. Can anyone give me some concrete...
19
2895
by: Shwetabh | last post by:
Hi, I have two tables: Code and Color. The create command for them is : create table Color( Partnum varchar(10), Eng_Color char(10), Span_Color char(20), Frch_Color char(20), CONSTRAINT...
52
6269
by: MP | last post by:
Hi trying to begin to learn database using vb6, ado/adox, mdb format, sql (not using access...just mdb format via ado) i need to group the values of multiple fields - get their possible...
4
2447
by: Dave White | last post by:
Hello Everyone, I have created two tables to track my students' lessons. Each student is responsible for most, but not all. of the lessons. I've tried a junction table but I can't figure out...
4
1901
daoxx
by: daoxx | last post by:
Hi I'm using MS Access 2002 / VBA 6. I've a database with several tables / forms, and some code. Everything is working fine. I have 2 fields in each table. Each of these fields contain names...
0
7202
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
7086
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
7280
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
1
6991
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
7460
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
1
5014
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
4672
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
3167
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
1512
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.