Example #1
0
        /// <summary>
        /// create a user
        /// </summary>
        /// <param name="email"></param>
        /// <param name="pass"></param>
        /// <param name="destroy">Whether or not a user should be destroyed if exists. If false and user exists no user destroy/recreation happens</param>
        /// <returns></returns>
        private async Task <MapHiveUser> CreateUserInternalAsync(string email, string pass, bool destroy)
        {
            //when creating org, need a user and if it exists, need to clean it up
            var user = await _db.Users.FirstOrDefaultAsync(u => u.Email == email);

            if (user != null && destroy)
            {
                await user.ForceDestroyAsync <MapHiveUser>(_db, user.Uuid);
            }

            if (user == null || destroy)
            {
                //recreate user
                user = new MapHiveUser
                {
                    Email             = email,
                    IsAccountVerified = true
                };

                //recreate user
                IDictionary <string, object> op = null;
                user.UserCreated += (sender, eventArgs) =>
                {
                    op = eventArgs.OperationFeedback;
                };

                await user.CreateAsync(_db);

                var userManager = MapHive.Core.Identity.UserManagerUtils.GetUserManager();
                var idUser      = await userManager.FindByEmailAsync(email);

                //once user is created, need to perform an update in order to set it as valid
                var confirmEmailToken = await userManager.GenerateEmailConfirmationTokenAsync(idUser);

                await userManager.ConfirmEmailAsync(idUser, confirmEmailToken);

                //also apply pass!
                await Auth.ForceResetPasswordAsync(idUser.Id, pass);
            }

            return(user);
        }
Example #2
0
        /// <summary>
        /// Standardises user creation
        /// </summary>
        /// <param name="dbCtx"></param>
        /// <param name="user"></param>
        /// <param name="emailStuff"></param>
        /// <param name="redirectUrl"></param>
        /// <returns></returns>
        public static async Task <MapHiveUser> CreateUser(DbContext dbCtx, MapHiveUser user,
                                                          Tuple <IEmailAccount, IEmailTemplate> emailStuff, string redirectUrl)
        {
            //Note: there are 2 options to send emails when creation a user account:
            //1. listen to UserCreated evt on the User object and then process email manually
            //2. grab the appropriate email template and email account, potentially adjust some email template tokens prior to creating a user and pass both sender account and email template to a user creation procedure

            //In this scenario a second approach is used

            //initial email template customisation:
            //{UserName}
            //{Email}
            //{RedirectUrl}
            var replacementData = new Dictionary <string, object>
            {
                { "UserName", $"{user.GetFullUserName()} ({user.Email})" },
                { "Email", user.Email },
                { "RedirectUrl", redirectUrl }
            };

            emailStuff?.Item2.Prepare(replacementData);

            return(await user.CreateAsync(dbCtx, CustomUserAccountService.GetInstance("MapHiveMbr"), emailStuff?.Item1, emailStuff?.Item2));
        }
Example #3
0
        protected virtual async Task Handle_AddUser(Dictionary <string, string> args)
        {
            var cmd = GetCallerName();

            if (GetHelp(args))
            {
                Console.WriteLine($"'{cmd}' : adds a user to the system");
                Console.WriteLine($"syntax: {cmd} space separated params: ");
                Console.WriteLine("\t[e:email]");
                Console.WriteLine("\t[p:pass]");
                Console.WriteLine("\t[s:slug] user's slug");
                Console.WriteLine("\t[o:{presence}] whether or not user is an org user");
                Console.WriteLine();
                Console.WriteLine($"example: {cmd} e:[email protected] p:test");
                return;
            }

            var email     = ExtractParam("e", args);
            var pass      = ExtractParam("p", args);
            var slug      = ExtractParam("s", args);
            var isOrgUser = ContainsParam("o", args);

            if (string.IsNullOrEmpty(email) || string.IsNullOrEmpty(pass))
            {
                ConsoleEx.WriteErr("User name and pass cannot be empty!");
            }

            ConsoleEx.WriteLine($"Creating user: '******' with the following pass: '******'; user is org user: {isOrgUser}; slug: {slug}", ConsoleColor.DarkYellow);

            //need a valid user to create a Core.Base object
            Server.Core.Utils.Identity.ImpersonateGhostUser();

            var user = new MapHiveUser
            {
                Email     = email,
                Slug      = slug,
                IsOrgUser = isOrgUser
            };


            //Note: db context uses a connection defined in app cfg.
            //TODO - make it somewhat dynamic!
            try
            {
                //destroy a previous account if any
                await DestroyUser <MapHiveUser>(email, new MapHiveDbContext("MapHiveMeta"), CustomUserAccountService.GetInstance("MapHiveMbr"));

                IDictionary <string, object> op = null;
                user.UserCreated += (sender, eventArgs) =>
                {
                    op = eventArgs.OperationFeedback;
                };

                await user.CreateAsync(new MapHiveDbContext("MapHiveMeta"), CustomUserAccountService.GetInstance("MapHiveMbr"));

                //once user is created, need to perform an update in order to set it as valid
                user.IsAccountVerified = true;
                await user.UpdateAsync(new MapHiveDbContext("MapHiveMeta"), CustomUserAccountService.GetInstance("MapHiveMbr"), user.Uuid);

                //and also need to change the pass as the default procedure autogenerates a pass
                CustomUserAccountService.GetInstance("MapHiveMbr")
                .ChangePassword(user.Uuid, (string)op["InitialPassword"], pass);

                ConsoleEx.WriteOk($"User '{email}' with the following pass: '******' has been created.");
                Console.WriteLine();
            }
            catch (Exception ex)
            {
                HandleException(ex);
                return;
            }
        }
        /// <summary>
        /// Creates a maphive user account
        /// </summary>
        /// <param name="email"></param>
        /// <param name="pass"></param>
        /// /// <param name="slug"></param>
        /// <returns></returns>
        protected async Task <MapHiveUser> CreateUserAsync(string email, string pass, string slug)
        {
            var user = new MapHiveUser
            {
                Email = email,
                Slug  = slug
            };

            ConsoleEx.WriteLine($"Creating user: '******' with the following pass: '******'; slug: {user.GetSlug()}", ConsoleColor.DarkYellow);

            if (RemoteMode)
            {
                user = await CreateUserRemoteAsync(email, pass, slug, true);
            }
            else
            {
                //need a valid user to create a Core.Base object
                Cartomatic.Utils.Identity.ImpersonateGhostUser();


                //Note: db context uses a connection defined in app cfg.
                //TODO - make it somewhat dynamic!
                try
                {
                    //destroy a previous account if any
                    using (var dbCtx = GetMapHiveDbContext())
                    {
                        await DestroyUserAsync <MapHiveUser>(email, dbCtx);

                        IDictionary <string, object> op = null;
                        user.UserCreated += (sender, eventArgs) =>
                        {
                            op = eventArgs.OperationFeedback;
                        };

                        await user.CreateAsync(dbCtx);


                        //and also need to change the pass as the default procedure autogenerates a pass
                        var userManager = MapHive.Core.Identity.UserManagerUtils.GetUserManager();
                        var idUser      = await userManager.FindByEmailAsync(email);

                        await userManager.ChangePasswordAsync(idUser, (string)op["InitialPassword"], pass);

                        //once user is created, need to perform an update in order to set it as valid
                        user.IsAccountVerified = true;
                        await user.UpdateAsync(dbCtx, user.Uuid);
                    }
                }
                catch (Exception ex)
                {
                    HandleException(ex);
                    return(null);
                }
            }

            ConsoleEx.WriteOk($"User '{email}' with the following pass: '******' has been created.");
            Console.WriteLine();

            return(user);
        }