How can I assigned roles automatically in webmatrix - database

I want to assigned new registered users automatically the role of Member in Database.

WebMatrix sample Starter Site app - Account\Register.cshtml
Tested and it works.
// Check if user already exists
var user = db.QuerySingle("SELECT Email FROM UserProfile WHERE LOWER(Email) = LOWER(#0)", email);
if (user == null) {
// Insert email into the profile table
db.Execute("INSERT INTO UserProfile (Email) VALUES (#0)", email);
//Roles have already been added to webpages_Roles table
//Logic to determine role for e-mail (user) being added for the first time
if (email == "John#gmail.com") {
var userName=email;
var roleName="Administrator";
Roles.AddUserToRole(userName, roleName);
}
else if (email == "Greg#gmail.com") {
var userName=email;
var roleName="Guest";
Roles.AddUserToRole(userName, roleName);
}
else {
//All others are assigned the role of Member
var username=email;
var roleName="Member";
Roles.AddUserToRole(userName, roleName);
}

In your register code, which in the sample WebMatrix app is found in the file Account\Register.cshtml, there's if statements that first determine that the information being submitted is valid and then that the email doesn't exist in your database. Within those if statements, if the user is successfully created, then you can add code like:
// boy, is this a hack, Member = RoleID 8
db.Execute("INSERT INTO webpages_UsersInRoles (UserId, RoleID) VALUES( #0, 8 )", UserID);
Now I used 8 as an example. Look in your webpages_Roles table. This has a list of RoleNames and their RoleID. If you've already created the Member role, it will be in this table. Use the corresponding RoleID instead of the 8 that I used.

Related

I need to get the user id from my users table in DynamoDB?

I'm using AWS Amplify on my react native app
I want to do something seemingly so simple, but i am a bit lost as to how to do it: I have a table with user information already in it. Here's the Schema:
type Users #model {
id: ID!
userName: String
firstname: String
weblink: String
email: String
mobileNum: String
.
.
.
}
//Here's my Query.js
export const getUsers = `query GetUsers($id: ID!) {
getUsers(id: $id) {
id
userName
firstname
weblink
email
.
.
.
}
}
`;
This table is populated in DynamoDB when i check my AWS console. What i need is to be able to get the id from the table using the userName (not vice versa). The id is generated when i createUser() and it's used throughout my app to get all my user's information. However when a user signs in on a new phone, this id isn't available anymore. So when they sign in via Cognito, i do know the userName and all i need to do is retrieve this id.
Normally i would do this, but obviously i want to do the reverse and use my unique userName to get the id:
const userData = await API.graphql(graphqlOperation(GetUsers, { id: usersId })); //usersId = "3b0dae-24j5-4401-95a6-11seyhaf1g131"
Any idea how i can get my users id?
Assuming that the userName is unique, create a Global Secondary Index with userName as the partition key. Project, at least, the id attribute into the index. Finally, search that index instead of the underlying table.
Alternatively, if you never need to lookup by id, consider making userName the partition key and id a regular attribute.

Save different data for same user on different Guilds

I set up a stats command as follows
[Command("Stats")]
public async Task StatsOther(SocketUser socketUser = null)
{
if (socketUser == null)
{
socketUser = Context.User;
}
var account = UserAccounts.GetAccount(socketUser);
await Context.Channel.SendMessageAsync($"Hey {socketUser.Mention}, You have {account.Size} long sandwhiches and {account.XP} XP.");
}
And the class UserAccounts searches if there exists in our database a socketUser with the ID property. Now say the same user in on different guild I need to store different data for him but the socketUser.ID will be the same no matter the guild. So when the user tries to use stats command he will see the same data irrespective of the guild he is in right now.
Here is where UserAccounts.GetAccount leads and does its thing,
public static UserAccount GetAccountFromID(ulong ID)
{
var result = from a in accounts
where a.ID == ID
select a;
var FoundAccount = result.FirstOrDefault();
if (FoundAccount == null)
{
FoundAccount = CreateUserAccount(ID);
}
return FoundAccount;
}
Clearly the linq query is checking for IDs and they happen to be the same for a user no matter the guild.
I tried using SocketGuildUser but sadly a socketGuildUser.ID is also independent of the guild. So I am unable to store different data for the same user from different guilds. Using the latest beta available.
How can I achieve this.
You could make use of a Dictionary implemented for each user. Where each user have its own Dictionary<GuildID, Data>.
And on the SQL side (if you are using SQL), you could have a new table, where it has a foreign key constrain on the User ID, and has a Guild ID too.
(The foreign key constrain on userID might not be needed if none of the user's stats is shared between all guilds; Aka you just have a SQL-table which you can do a SELECT stuff FROM tableName WHERE userID = ? AND guildID = ?)

Can't select User ID from Salesforce

I'm trying to select user's subordinates from Salesforce, but a simple query
SELECT Id FROM User WHERE ManagerId=xxxxxxxxx
returns bunch of null values, when I run
SELECT Id,Name FROM User WHERE ManagerId=xxxxxxxx
I get the correct names, still no IDs.
Is this a permission issue? I can't find anything when I login to portal.
I'm running the queries via API on Sandbox environment.
Try this (both works for me allways):
Id myId = [Select Id From User Where Username = 'myUserName'].Id;
System.debug('#### myId: ' + myId);
List<User> myIdList = [Select Id From User Where Username = 'myUserName' Limit 1];
System.debug('#### myId from list: ' + myIdList[0].Id);
Portal Licence doesn't allow to query User. However you have still access to the name of the user through OwnerId, CreatedById, LastModifiedById using in an inputfield.
i.e :
If you want to have access to user through the portal you need a custom object and synchronise your records with User by trigger.

Entity Framework foreign key default value

I have tables Users and Roles.
In Users table I have default value for RoleId column.
But I don't how I can make it work in EF4, for example, if I set RoleId property as DatabaseGeneratedOption.Computed
_repository.Save(new User()
{
Login = "asdasd",
Name = "sadqwqwd",
Password = "wqdwqd"
});
new user will be insert with default value, but if I want set RoleId in code
_repository.Save(new User()
{
Login = "qqasdasd",
Name = "qqsadqwqwd",
Password = "qqwqdwqd",
RoleId = 2
});
it will ignore my RoleId value and set again default value.
If I remove DatabaseGeneratedOption.Computed option I cant insert user object wihtout RoleId value.
How I can resolve this problem? Thanks in advance.
Properties with DatabaseGeneratedOption.Computed option will be ignored by EF in insert/update operations and can be set on database side only.
Remove DatabaseGeneratedOption.Computed option from RoleId property and set default value in the constructor of User class.

manipulating email address with a sql stored procedure

I'm trying to figure out how to best manipulate an email adress within this (part of) stored procedure.
IF(#LoginName IS NULL OR #LoginName = '')
BEGIN
set #LoginName = #Email
END
What is happening: a user is saved but If the email already exist the email address receives a number at the end of it. for example: test#email.com12
once this email is written to the database this stored procedure saves the email as the login name. however I want the login name to be just "test#email.com" without the "12" at the end of it. What would the best way to do this be?
Thanks for any help, my sql skills are not very good.
Here is the javascript that alters the email address and writes it to the database
function userEdit()
{
if (typeof userEdit.counter == 'undefined')
{
userEdit.counter = 0;
}
userEdit.counter++;
var change = document.getElementById("txtEmail").value;
var email = change + "" +userEdit.counter;
var telephone = document.getElementById("txtTelephone").value;
var mobile = document.getElementById("txtMobile").value;
var loginUser = document.getElementById("chkLogin").checked;
var password = document.getElementById("txtPassword").value;
var confirmpassword = document.getElementById("txtConfirmPassword").value;
card.user.update(wizard.stateInfo.activeUserID, wizard.stateInfo.activeUser, email, telephone, mobile, loginUser, password);
}
The best practice here is to keep a record of both the modified and unmodified email addresses, in two different variables. Then you don't need to "undo" the changes, you just pick the variable that is suitable to your needs at that time.

Resources