YAFLogo

johnw86
  • johnw86
  • 56% (Neutral)
  • YAF Camper Topic Starter
10 years ago
Hello all,

http://stackoverflow.com/questions/3353258/programmatically-setting-display-name-in-yaf  but at the bottom Jaban mentioned the latest code has a new method for doing this, does anyone know where this call is?

Thanks in advance

John

Sponsor

tha_watcha
  • tha_watcha
  • 100% (Exalted)
  • YAF.NET Project Lead 🤴 YAF Version: 3.0.3
10 years ago
Originally Posted by: johnw86 

Hello all,

http://stackoverflow.com/questions/3353258/programmatically-setting-display-name-in-yaf  but at the bottom Jaban mentioned the latest code has a new method for doing this, does anyone know where this call is?

Thanks in advance

John

Code snipped from the edit user profile page

// save remaining settings to the DB
            LegacyDb.user_save(
              this.currentUserID,
              this.PageContext.PageBoardID,
              null,
              displayName,
              null,
              this.TimeZones.SelectedValue.ToType<int>(),
              language,
              culture,
              theme,
              this.SingleSignOn.Checked,
              editor,
              this.UseMobileTheme.Checked,
              null,
              null,
              null,
              this.DSTUser.Checked,
              this.HideMe.Checked,
              null);
johnw86
  • johnw86
  • 56% (Neutral)
  • YAF Camper Topic Starter
10 years ago
Thanks for that I had seen that call I was hoping that there may be another call without the need to pull all those details out from the user to update the display name.

Thanks though.

John

johnw86
  • johnw86
  • 56% (Neutral)
  • YAF Camper Topic Starter
10 years ago
Hi,

Users details are being synced in the forum via a handler as I need to update the display name when it is updated in an external system, what is the best way to retrieve these properties for the LegacyDb.user_save as I can't see them on the MembershipUser or YafUserProfile

Thanks again,

John

tha_watcha
  • tha_watcha
  • 100% (Exalted)
  • YAF.NET Project Lead 🤴 YAF Version: 3.0.3
10 years ago
Originally Posted by: johnw86 

Thanks for that I had seen that call I was hoping that there may be another call without the need to pull all those details out from the user to update the display name.

Thanks though.

John

You could add a new stored procedure that updates only the display name

CREATE procedure [{databaseOwner}].[{objectQualifier}user_update_displayname](
	@UserID				int,
	@DisplayName		nvarchar(255) = null)
AS
begin
	
		DisplayName = (CASE WHEN (@DisplayName is not null) THEN  @DisplayName ELSE DisplayName END)
end
GO

and the method

public static void user_update_displayname(
            [NotNull] object userID, 
            [NotNull] object displayName,)
        {
            using (var cmd = MsSqlDbAccess.GetCommand("user_update_displayname"))
            {
                cmd.CommandType = CommandType.StoredProcedure;
                cmd.Parameters.AddWithValue("UserID", userID);
                cmd.Parameters.AddWithValue("DisplayName", displayName);
                MsSqlDbAccess.Current.ExecuteNonQuery(cmd);
            }
        }
johnw86
  • johnw86
  • 56% (Neutral)
  • YAF Camper Topic Starter
10 years ago
Yeah that was going to be my next move if there wasn't a simpler way of doing it. I was just trying my best to leave the source code/db alone for upgrades etc.

Thanks

John

tha_watcha
  • tha_watcha
  • 100% (Exalted)
  • YAF.NET Project Lead 🤴 YAF Version: 3.0.3
10 years ago
Originally Posted by: johnw86 

Yeah that was going to be my next move if there wasn't a simpler way of doing it. I was just trying my best to leave the source code/db alone for upgrades etc.

Thanks

John

No i think it is the only way to do it. and you dont have to modify yaf itself,

- you could put the user_update_displayname method in your own code

- you can add custom procedures via the yaf installer without editing the procedures.sql. Simply add a new file in the Install folder called "custom.sql" where you can put all custom sql stuff.

johnw86
  • johnw86
  • 56% (Neutral)
  • YAF Camper Topic Starter
10 years ago
Perfect that's exactly what I will do.

John

johnw86
  • johnw86
  • 56% (Neutral)
  • YAF Camper Topic Starter
10 years ago
Just another quick question on this as I'm creating the users programatically and setting their display name what is the best way to get the userId to do the display name update. I have tried:

int userId = UserMembershipHelper.GetUserIDFromProviderUserKey(user.ProviderUserKey);

But this seems to return 0, any other methods for retrieving the userId.

Thanks

John

bbobb
  • bbobb
  • 100% (Exalted)
  • YAF Developer
10 years ago
If you pass a correct providerUserKey it should not return 0, it seems you have an incorrect logic.

You don't need create users programmaticaly as they are put in YAF users table after their first visit.

The DisplayName = UserName in the beginning. So you can simply check if they are the same in your YAF control and if not - trigger user_save things.

johnw86
  • johnw86
  • 56% (Neutral)
  • YAF Camper Topic Starter
10 years ago
Hi,

Im working on a custom AD membership and single sign on provider so I'm validating users against these two providers and if it's their first visit to the site I'm creating them as a user programmatically otherwise finding their account based on the username and logging them in.

To create the user im using:

YAF.Providers.Profile.DB profile = new YAF.Providers.Profile.DB();

var user = provider.CreateUser(params);

But then when I call int userId = UserMembershipHelper.GetUserIDFromProviderUserKey(user.ProviderUserKey); that is returning 0. Is there any other way I can get the userID out or is it a case of that logic above is incorrect?

Cheers,

John

bbobb
  • bbobb
  • 100% (Exalted)
  • YAF Developer
10 years ago
Oh, you'll have multiple pitfalls with AD. I can help with open source code only in difficult cases like this.
johnw86
  • johnw86
  • 56% (Neutral)
  • YAF Camper Topic Starter
10 years ago
No problem, Im not actually using the AD membership I'm just authenticating users against that then logging them into a yaf account which has been associated with the AD user so still using YAF mem provider.

Found another method I needed to call after creating the user which seems to give me the newly created userID

int? userID = RoleMembershipHelper.CreateForumUser(user, YafContext.Current.PageBoardID);

John