Extend User authentication object in Azure Mobile Services - azure-mobile-services

Is it possible to add additional properties to the User object on the server in WAMS? I would like to store the Id primary key of my User table for (secure) use in my table scripts. At the moment the only id is the vendor specific authentication Id, but I'd like to be able to allow users to choose an authentication method. Currently my (simplified) table design is as follows:
User table:
id
googleId
twitterId
facebookId
name, etc...
League table
id
userId
name, etc
I'd like to store the user primary key in the userId field on the league table, and then query it to ensure that users only get to see leagues they created. At the moment, the user object in table scripts sends through a User object with the Google/Twitter/Windows authentication token and I have to do a query to get the primary key userID, everytime I want to carry out an operation on a table with a userId column.
Ideal solution would be that when the Insert script on my User table is called on registrations and logins I can do:
// PSEUDO CODE
function insert(item, user, request) {
var appUserId;
Query the user table using the user.userId Google/Twitter/Facebook id
If user exists {
// Set a persisted appUserId to use in all subsequent table scripts.
user.appUserId = results.id;
} else {
Set the GooTwitFace columns on the user table, from user.userId
insert the user then get the inserted record id
// Set a persisted appUserId to use in all subsequent table scripts
user.appUserId = insertUserPK;
}
}
Then, in subsequent table scripts, I'd like to use user.appUserId in queries

If all you are trying to do is authorize users to only have access to their own data, I'm not sure you even need the "user" table. Just use the provider-specific userId on the user object to query your "league" table (making sure the userId column is indexed). The values will be provider-specific, but that shouldn't make any difference.
If you are trying to maintain a notion of a single user identity across the user's Google/Facebook/Twitter logins, that's a more complicated problem where you would need a "user" table and the kind of lookup you are describing. We hope to ship support for this scenario as a feature out of the box. It is possible (but fairly messy) to do this yourself, let me know if that's what you're trying to do.

Related

Nest TypeORM Postgres update user's column('number of posts') based on the userId in the Posts Table

I'm wondering if it's possible to auto update the User's column('number of posts') if the Posts table updates. The Post entity has a ManyToOne relation with User('userId'). Is there a way to make the User Table "listen" to the Post Table and automatically updates the number of post column, or i need to write it in the post service create function to do so. I'm new to sql so i'm just trying new stuff. I'm using NestJS,typeORM, Postgres and Graphql
#Kendle's answer does work and has the advantage of pushing the computation and complexity down onto your DB server. Alternatively, you can keep that logic in the application by leveraging TypeORM's Subscribers functionality. Documentation can be found here.
In your specific use case, you could register a subscriber for your Post entity implementing afterInsert and afterRemove (or afterSoftRemove if you soft delete posts) to increment and the decrement the counter respectively.
You don't want to duplicate that data. That's the whole idea of a relational database that different data is kept in different tables.
You can create a view if you want to avoid typing a query with a JOIN each time.
For example you might create the view below:
CREATE VIEW userPosts AS
SELECT
user.id,
user.name,
COUNT(posts.id)
FROM users
LEFT JOIN posts ON user.id = posts.user_id
ORDER BY user.id;
Once you have created the view your can query it as if it were a table.
SELECT * FROM userDate WHERE id = '0001';
Of course I don't have your table definitions and data so you will need to adapt this code to your tables.

How to add non unique value into a unique list while still allowing value to be updatable

I have a simple database schema with User records
User
id - uuid pk
email - not null
List
id - uuid pk
List Entry
id - uuid pk
userId - uuid references User (id)
listId - uuid references ListEntry (id)
The trick is the system needs to guarantee uniqueness of User emails within a list while still allowing for the email of the User record to be updatable.
An obvious solution is the add email to the List Entry table with a unique index, but, this runs into issues with the requirement that the email is updatable. Especially because the application treats a User as its own standalone entity that can be modified in a place that does not know about Lists and ListEntries.
I do control the code executing updates and I have considered attempting a solution using serializable transactions to prevent duplicates, but, that seems worse than trying to keep the two email fields in sync.

Geting user details using foreign key from res.users in Odoo

I have a model Employee and have included a foreign key to store the user object from the res.users table using the following code in my model:
user_id = fields.Many2one('res.users', string='user id', default=lambda self: self.env.user)
In my form, I am already capturing the user object.
Now, I want to display the user object's details (id, name, email) using the foreign key (that is the user object) in my form view.
The <field name="user_id"/> in my form view would display the user name of the stored object. I want to display different fields for id, email.
you can create a related field for user_id,
x = fields.Char(related='user_id.email')
like this you can access the all field values of related record.

JPA - Join Two tables

I have two tables, namely
USER_ROLE {user_id, Role} PK {user_id, role}
ROLE_PERMISSION {role, permission} PK {role, permission}
A User can have multiple Roles.
A Role can be mapped to multiple
Permissions.
I have a entity - USER that maintains information about the User. This info is fetched via LDAP (not DB) on first login. Now, for my authorization aspects, I need to also fetch dtls on User's permissions from above mentioned tables.
So I would imagine adding attributes to my existing USER entity
USER {
user_id,
first_name,
last_name,
etc
// Authorization
List<String> roles;
List<String> permissions;
}
Can someone pls help how I can use JPA to populate the roles and permissions Lists? Looked over internet, can't figure it out. thanks
I would create a USER table in your database and map it to a User object with the role and permissions. The User object then would include additional LDAP data.
Without a USER table you have nothing to map to.
Otherwise just query for the database using native SQL queries and populate your LDAP user object yourself.

cakephp passing a variable to another controller

I am hoping to get pointed in the right direction. I want to pass a variable from one controller into another controller.
what I want to do is have a person register a business then they are taken to a form to register a user. a business is a different controller/table to a user however the user requires the id/primary key of the business as a foreign key in the user table. How would I go about changing controllers and carrying the foreign key over?
the primary key for the business table is an autogenerated/autoincremented int in the database
i am unsure on how I would approach this but have a feeling it is to do with session data?
Why dont you pass the id in the url?. I'd do it like this:
Display /business/add. This is the form used to create a "business".
After saving the business in your controller, redirect to /business/add_user/123 (where "123" is the id of your business). This page displays and saves the users. Since you have passed the business_id in the url you'd have to add it as a foreign key manually into the $this->request->data before saving the user.
Of course that inside the controller of /business/add_user/123 you should verify a few thing: check if the business_id was passed as parameter in the url, check if the business exist, maybe check that the connected user was the one that created the businnes, etc
Hope this helps