Existing website that requires multiple users to access the same data - php

I have an existing website, that doesn't use any specific framework. This project is much older and is slowly being evolved, which is somewhat of a nightmare really.
Currently, I am trying to implement a better solution to current users to have 'assistants' to their accounts.
The current data resides like this (users & contacts tables):
**users table**
userId, email, password...
1 test#test.com, pa$$word
2 ass#test.com, pa$$word
**contacts table**
contactId, userId, fName, lName...
1, 1, john, doe
I am trying to figure out how to modify my site to enable userId's (1&2), to be able to access this contact.
Instead of starting over, any direction or samples that I could glean from on how to solve this issue of mine? Any help would be greatly appreciated.

Remove the userId column from the contacts table and create a new table contacts_users with two columns, contactId and userId.This is called a PIVOT TABLE and allows many-to-many relationships like what you are describing.

Related

User Level Logic

I have a project that I am working on and I've been researching about having User Levels and the recommended & secure ways of doing it.
The project will have some more complex stuff like groups, users, pages, chat and I don't want to make some changes now and be required to re-change the logic when I will add the other features.
I would like some advice or help on this, I would appreciate it very much.
The way I was thinking was either
Have a table named userlevels (for example) with 3 columns ID, Name, Permissions
1, user
2, Administrator, {"admin": 1} with json
OR
Have an additional row with permissions in the normal users table, and have a row for example
is_admin [0,1]
Is it okay to do it like this ? If you have any other ideas for me I would honestly appreciate it very much.
Thanks in Advance!
I'll propose a more normalized solution that should be completely flexible going forward.
A table of users:
UserID (PK)
UserName
A table of permissions
PermissionId (PK)
Permission
A junction table to resolve the many-to-many relationship between users and permissions
UserID (FK to users)
PermissionId (FK to permissions)

Mysql design. Two types of users, two different profiles

I want to design a DB which will be connected to PHP Application. In the app there are two types of users: company and person. Some functionality like adding articles will be done by both so in other tables there are author_id columns. So firstly I decided to create user column.
That's easy: id, username, password, role, active, created where role defines whether user is person or company.
Now I want to add profile table or profile tables depends on what you'd suggest (joined with the previous table by adding profile_id column there).
Both roles have different fields, which are required during registration.
The easiest thing would be to create one table with all required fields for both roles, allow them NULL values and in the PHP app (made in Yii Framework in this case) define requirements for each role in models.
The nicest thing would be to create separate tables for both roles BUT the questions is how to connect these two tables to one table using Foreign Key? Is it even possible. I know I may omit foreign key creation then based on role choose table, and from that table choose profile_id.
Or maybe you have another solution to my problem.
Thanks in advance for replies.
Adrian
You need an intermediary between the page and the database to assign the user to a group that has specific privileges. It's usually accomplished with a user-group-role design.
You can have a table for users system info (username , pass ...), and another for users profile (firstname , birthday ...), and another for groups(superuser , ...).
where user table can have multiple groups: user:one->group:many
user can have one profile user:one->profile:one
I think this is a decent solution.

How to keep data separate for businesses or groups of customers?

I've done quit a bit of programming with php/mysql on small scale personal projects. However I'm working on my first commercial app that is going to allow customers or businesses to log in and perform CRUD operations. I feel like a total noob asking this question but I have never had to do this before and cannot find any relevant information on the net.
Basically, I've created this app and have a role based system set up on my data base. The problem that I'm running into is how to separate and fetch data for the relevant businesses or groups.
I can't, for example, set my queries up like this: get all records from example table where user id = user id, because that will only return data for that user and not all of the other users that are related to that business. I need a way to get all records that where created by users of a particular business.
I'm thinking that maybe the business should have an id and I should form my queries like this: get all records from example where business id = business id. But I'm not even sure if that's a good approach.
Is there a best practice or a convention for this sort data storing/fetching and grouping?
Note:Security is a huge issue here because I'm storing legal data.
Also, I'm using the latest version of laravel 4 if that's any relevance.
I would like to hear peoples thoughts on this that have encountered this sort problem before and how they designed there database and queries to only get and store data related to that particular business.
Edit: I like to read and learn but cannot find any useful information on this topic - maybe I'm not using the correct search terms. So If you know of any good links pertaining to this topic, please post them too.
If I understand correctly, a business is defined within your system as a "group of users", and your whole system references data belonging to users as opposed to data belonging to a business. You are looking to reference data that belongs to all users who belong to a particular business. In this case, the best and most extensible way to do this would be to create two more tables to contain businesses and business-user relations.
For example, consider you have the following tables:
business => Defines a business entity
id (primary)
name
Entry: id=4, name=CompanyCorp
user => Defines each user in the system
id (primary)
name
Entry: id=1, name=Geoff
Entry: id=2, name=Jane
business_user => Links a user to a particular business
user_id (primary)
business_id (primary)
Entry: user_id=1, business_id=4
Entry: user_id=2, business_id=4
Basically, the business_user table defines relationships. For example, Geoff is related to CompanyCorp, so a row exists in the table that matches their id's together. This is called a relational database model, and is an important concept to understand in the world of database development. You can even allow a user to belong to multiple different companies.
To find all the names of users and their company's name, where their company's id = 4...
SELECT `user`.`name` as `username`, `business`.`name` as `businessname` FROM `business_user` LEFT JOIN `user` ON (`user`.`id` = `business_user`.`user_id`) LEFT JOIN `business` ON (`business`.`id` = `business_user`.`business_id`) WHERE `business_user`.`business_id` = 4;
Results would be:
username businessname
-> Geoff CompanyCorp
-> Jane CompanyCorp
I hope this helps!
===============================================================
Addendum regarding "cases" per your response in the comments.
You could create a new table for cases and then reference both business and user ids on separate columns in there, as the case would belong to both a user and a business, if that's all the functionality that you need.
Suppose though, exploring the idea of relational databases further, that you wanted multiple users to be assigned to a case, but you wanted one user to be elected as the "group leader", you could approach the problem as follows:
Create a table "case" to store the cases
Create a table "user_case" to store case-user relationships, just like in the business_user table.
Define the user_case table as follows:
user_case => Defines a user -> case relationship
user_id (primary)
case_id (primary)
role
Entry: user_id=1, case_id=1, role="leader"
Entry: user_id=2, case_id=1, role="subordinate"
You could even go further and define a table with definitions on what roles users can assume. Then, you might even change the user_case table to use a role_id instead which joins data from yet another role table.
It may sound like an ever-deepening schema of very small tables, but note that we've added an extra column to the user_case relational table. The bigger your application grows, the more your tables will grow laterally with more columns. Trust me, you do eventually stop adding new tables just for the sake of defining relations.
To give a brief example of how flexible this can be, with a role table, you could figure out all the roles that a given user (where user_id = 6) has by using a relatively short query like:
SELECT `role`.`name` FROM `role` RIGHT JOIN `user_case` ON (`user_case`.`role_id` = `role`.`id`) WHERE `user_case`.`user_id` = 6;
If you need more examples, please feel free to keep commenting.

Basic MYSQL/PHP Event Registration (Database Relationships)

I am relatively new to to database relationships, and I would love some advice on setting up my database for a basic registration for sessions.
GOAL:
Post a new session that people can sign up for. Maximum of 8 people per session. Admin side can see those who have signed up.
Here is what I currently have from a front end (the signup box at the bottom is just there temporarily as I work with this):
I have the backend working to create a new training session on POST:
<?php
session_start();
include ('global/db/connection.php');
$session_name = $_POST['session_name'];
$session_description = $_POST['session_description'];
$session_date = $_POST['session_date'];
$result=MYSQL_QUERY("INSERT INTO trainingsessions (id,session_name,session_description,session_date,session_open_slots,session_booked_slots)".
"VALUES ('NULL', '$session_name', '$session_description', '$session_date', '8', '0')");
header("location: admin.php");
?>
I also have been able to query the table and display the results in the table above. What I am trying to figure out is what sort of relationship and tables I need to create between the users and then displaying the appropriate amount of slots that are still open.
Very basic, nothing fancy at the moment. Just trying to get some fundamentals in with not luck thru other searches. Hope this makes sense, and thanks for your help!
You will have a many to many relationship between users and sessions (presumably). In essence each user could be assigned to many sessions, and each session will have multiple users. For that reason, you need an intersecting table that creates a composite key (the primary key is defined by multiple fields). For example, I would make the following tables:
Table: user
Columns: ID (primary key), Username, email
Table: sessions
columns: ID (primary Key), Name, Description, Date
Table: user_sessions
columns: user_id, session_id
Using this structure, users can have multiple sessions and sessions can have multiple users.

Relationship with Tables MySQL

I have some small issue with relationships with tables as below:
I have created a login and registration script which has the following
table name: members:
fields: member_id, firstname, lastname, login, password
Now i have made another table with this
table name: phone
fields: member_id, phoneid, name, number, prefix, time,total
I want to make a form whereby a admin can select the name of the client from a drop down list, and then add a record such as the number called, number prefix, total time and the amount for that period.
I dont know how to do this, please help me by creating a script or help me how to go about this.
So all the time a admin makes a form on Client A it gets added to a new row on the phone table, then i will just add a call script on the client side where they can see all the records that they have done.
Thanks please assist.
Regards
Your db design seems fundamentally flawed - the is no association between the members and the phone_numbers tables. Add a FK (member_id) to phone_numbers table.
Regards creating the front-end, there are quite a lot of libraries that have data stores (we use ExtJS), and upon flushing the store you can do the persistence with php.
Hope this helps!

Categories