Copied to clipboard

Flag this post as spam?

This post will be reported to the moderators as potential spam to be looked at


  • Matt 354 posts 826 karma points
    Mar 19, 2020 @ 15:23
    Matt
    0

    members sign up only allow certain email extension

    Hi all,

    So I'm using the partial snippet which comes with Umbraco, and everything works fine, users are able to sign up.

    However, I only want to allow a certain time of email extension e.g [email protected]

    How could I achieve this?

    Here is my email code block;

                <div class="col-xs-12">
                    @Html.TextBoxFor(m => registerModel.Email, new { @class = "text-field w-input" })
                     @Html.ValidationMessageFor(m => registerModel.Email)
                </div>
    

    Thanks

  • Søren Gregersen 441 posts 1884 karma points MVP 2x c-trib
    Mar 24, 2020 @ 17:52
    Søren Gregersen
    0

    Hi Matt,

    On MemberService there is an event called Saving

    When you hook into that, you will be able to cancel a member being created. This will allow you to "just" cancel the event, if the email-address does not meet your criteria.

    In umbraco 8 you need to implement this with a composer. The documentation contains an example on how to listen for an event on ContentService - https://our.umbraco.com/Documentation/Implementation/Composing/#example---creating-a-component-to-listen-for-contentservicesaving-events

    An example for your usecase whould be something like this

    using System.Linq;
    using Umbraco.Core;
    using Umbraco.Core.Composing;
    using Umbraco.Core.Events;
    using Umbraco.Core.Services;
    using Umbraco.Core.Services.Implement;
    
    namespace My.Website
    {
        public class MemberServiceEventsComposer : IUserComposer
        {
            public void Compose(Composition composition)
            {
                // Append our component to the collection of Components
                // It will be the last one to be run
                composition.Components().Append<MemberServiceEventsComponent>();
            }
        }
    
        public class MemberServiceEventsComponent : IComponent
        {
            // initialize: runs once when Umbraco starts
            public void Initialize()
            {
                MemberService.Saving += MemberService_Saving;
            }
    
            // terminate: runs once when Umbraco stops
            public void Terminate()
            { }
    
            private void MemberService_Saving(IMemberService sender, SaveEventArgs<IMember> e)
            {
                 foreach (var member in e.SavedEntities)
                 {
                        if(!member.IsApproved && !member.Email.EndsWith("examle.com")){
                            e.Cancel = true;
                            return;
                        }
    
                 }
            }
        }
    }
    

    This utilises the IsApproved flag, so members that are already created and approved, will not be validated.

Please Sign in or register to post replies

Write your reply to:

Draft