YAFLogo

louis
  • louis
  • 50.2% (Neutral)
  • YAF Forumling Topic Starter
12 years ago
Hi,

I'm new to Yaf and trying to create a user without using the form Register form. So, going through the Yaf classes to create a user in the database. Is this possible? Does the functionality even exist?

Thanks

Sponsor
tha_watcha
  • tha_watcha
  • 100% (Exalted)
  • YAF.NET Project Lead 🤴 YAF Version: 4.0.1 BETA
12 years ago
Yes of course its possible just look at the register.ascx.cs page how its done


MembershipCreateStatus status;

var pass = "Password"// Membership.GeneratePassword(32, 16);
var securityQuestion = "Security Question";
var securityAnswer = "Security Answer";
var userName = "UserName";

MembershipUser user = YafContext.Current.Get<MembershipProvider>().CreateUser(
                            userName, pass, email, securityQuestion, securityAnswer, true, null, out status);

// setup inital roles (if any) for this user
RoleMembershipHelper.SetupUserRoles(YafContext.Current.PageBoardID, userName);

// create the user in the YAF DB as well as sync roles...
                        int? userID = RoleMembershipHelper.CreateForumUser(user, YafContext.Current.PageBoardID);

// create empty profile just so they have one
YafUserProfile userProfile = YafUserProfile.GetProfile(userName);

// Set profile properties?!
userProfile.Homepage = "http://www.test.com";

// setup their inital profile information
 userProfile.Save();

if (YafContext.Current.Get<YafBoardSettings>().NotificationOnUserRegisterEmailList.IsSet())
{
// send user register notification to the following admin users...
SendRegistrationNotificationEmail(user);
}

// save the time zone...
 int userId = UserMembershipHelper.GetUserIDFromProviderUserKey(user.ProviderUserKey);

 LegacyDb.user_save(
                            userId,
                            YafContext.Current.PageBoardID,
                           UserName,
                            null,
                            email,
                            0,
                            null,
                            null,
                            null,
                            true,
                            null,
                            null,
                            null,
                            null,
                            null,
                            null,
                            null,
                            null);

bool autoWatchTopicsEnabled = YafContext.Current.Get<YafBoardSettings>().DefaultNotificationSetting ==
                                                      UserNotificationSetting.TopicsIPostToOrSubscribeTo;

                        // save the settings...
LegacyDb.user_savenotification(
                            userId,
                            true,
                            autoWatchTopicsEnabled,
                            YafContext.Current.Get<YafBoardSettings>().DefaultNotificationSetting,
                            YafContext.Current.Get<YafBoardSettings>().DefaultSendDigestEmail);