YAFLogo

McHine
  • McHine
  • 67.6% (Friendly)
  • YAF Forumling Topic Starter YAF Version: 4.0.0 RC4
20 days ago
Been a while, Watcha.

OK brief one, what's the code to insert a User into a role?

which variables need to be passed? 

thanks a lot!

Sponsor
tha_watcha
  • tha_watcha
  • 100% (Exalted)
  • YAF.NET Project Lead 🤴 YAF Version: 4.0.1 BETA
20 days ago
to add the user to a role (group)

​this.GetRepository<UserGroup>().AddOrRemove(userId, roleId, true);

 this.Get<IAspNetRolesHelper>().AddUserToRole(aspnetUser, roleName);
McHine
  • McHine
  • 67.6% (Friendly)
  • YAF Forumling Topic Starter YAF Version: 4.0.0 RC4
20 days ago
Thanks! so just to confirm: need to do both of those, one to add to the group and the other to add to the iasp roles?

It's not either or.

tha_watcha
  • tha_watcha
  • 100% (Exalted)
  • YAF.NET Project Lead 🤴 YAF Version: 4.0.1 BETA
19 days ago
Correct you need both.
McHine
  • McHine
  • 67.6% (Friendly)
  • YAF Forumling Topic Starter YAF Version: 4.0.0 RC4
18 days ago
Almost there, I'm not getting what aspnetUser is supposed to be.

trying to insert the userid, but that not obviously it:

Argument 1: cannot convert from 'int' to 'YAF.Types.Models.Identity.AspNetUsers'

so basically how do i call it?

Copying and pasting into the forum makes an absolute mess, btw, adds some kind of picture and then double pastes

see what appears below and the picture of the text area if it's different after posting......

 Screenshot 2025-04-23 231042.jpg You have insufficient rights to see the content.

Copying and pasting into the forum makes an absolute mess, btw, adds some kind of picture and then double pastesCopying and pasting into the forum makes an absolute mess, btw, adds some kind of picture and then double pastes

tha_watcha
  • tha_watcha
  • 100% (Exalted)
  • YAF.NET Project Lead 🤴 YAF Version: 4.0.1 BETA
18 days ago
you need to get the aspnetusers first before you call the method.

var aspnetUser  = await this.Get<IAspNetUsersHelper>().GetUserAsync(yafUser.ProviderUserKey)

Yes i am aware of the paste  editor issue this will be fixed in 3.2.9/4.0.1

McHine
  • McHine
  • 67.6% (Friendly)
  • YAF Forumling Topic Starter YAF Version: 4.0.0 RC4
10 days ago
Ah, Userkey, not userid.....

what's the call then to get the userkey if i have the userid?

and what's the reasoning behind using a userkey? seems like an id with extra steps?

wait wait i got it, it's aspnet and their ridiculously long useridkey. 

thanks a lot apprecaite the time and answer to the endless questions (there are less these days aren't there?)

so after adding to get the useridkey to this , it should work?

// Add user to the Owner role group

          BoardContext.Current.GetRepository().AddOrRemove(owner.UserID, 6, true);

//insert get userkey here

          // Get the AspNetUsers object for this user

          var aspNetUser = await BoardContext.Current.Get().GetUserAsync(owner.UserID); ().AddUserToRole(aspNetUser, "Owner");

          }         

tha_watcha
  • tha_watcha
  • 100% (Exalted)
  • YAF.NET Project Lead 🤴 YAF Version: 4.0.1 BETA
10 days ago
Yes that is correct.
McHine
  • McHine
  • 67.6% (Friendly)
  • YAF Forumling Topic Starter YAF Version: 4.0.0 RC4
9 days ago
Hi Watcha, sorry there's too many questions and me answering some of them myself in there.

i need:

the call then to get the userkey if i have the userid.

tha_watcha
  • tha_watcha
  • 100% (Exalted)
  • YAF.NET Project Lead 🤴 YAF Version: 4.0.1 BETA
8 days ago
It depends where you get the userid from. But you can get the userkey from the User table

​this.GetRepository<User>().GetById(userId).ProviderUserKeythis.GetRepository<User>().GetById(userId).ProviderUserKey