Esempio n. 1
0
        /// <summary>
        /// Creates a new member
        /// </summary>
        /// <param name="Name">Membername</param>
        /// <param name="mbt">Member type</param>
        /// <param name="u">The umbraco usercontext</param>
        /// <param name="Email">The email of the user</param>
        /// <returns>The new member</returns>
        public static Member MakeNew(string Name, string LoginName, string Email, MemberType mbt, User u)
        {
            if (mbt == null) throw new ArgumentNullException("mbt");

            var loginName = (!string.IsNullOrEmpty(LoginName)) ? LoginName : Name;

            if (string.IsNullOrEmpty(loginName))
                throw new ArgumentException("The loginname must be different from an empty string", "loginName");

            // Test for e-mail
            if (Email != "" && GetMemberFromEmail(Email) != null && Membership.Providers[UmbracoMemberProviderName].RequiresUniqueEmail)
                throw new Exception(string.Format("Duplicate Email! A member with the e-mail {0} already exists", Email));
            else if (GetMemberFromLoginName(loginName) != null)
                throw new Exception(string.Format("Duplicate User name! A member with the user name {0} already exists", loginName));

            // Lowercased to prevent duplicates
            Email = Email.ToLower();
            var newId = Guid.NewGuid();

            //create the cms node first
            var newNode = MakeNew(-1, _objectType, u.Id, 1, Name, newId);

            //we need to create an empty member and set the underlying text property
            var tmp = new Member(newId, true);
            tmp.SetText(Name);

            //create the content data for the new member
            tmp.CreateContent(mbt);

            // Create member specific data ..
            SqlHelper.ExecuteNonQuery(
                "insert into cmsMember (nodeId,Email,LoginName,Password) values (@id,@email,@loginName,'')",
                SqlHelper.CreateParameter("@id", tmp.Id),
                SqlHelper.CreateParameter("@loginName", loginName),
                SqlHelper.CreateParameter("@email", Email));

            //read the whole object from the db
            var m = new Member(newId);

            var e = new NewEventArgs();

            m.OnNew(e);

            m.Save();

            return m;
        }