Example #1
0
        internal async Task <SearchByIdModel> New(UsersUpdateModel model, HttpRequestMessage Request, string connetionString, bool isAdmin, string idAdminrole)
        {
            SearchByIdModel idUser = new SearchByIdModel();

            var appDbContext = Request.GetOwinContext().Get <ApplicationDbContext>();

            using (var identitydbContextTransaction = appDbContext.Database.BeginTransaction())
            {
                var UserManager = Request.GetOwinContext().GetUserManager <ApplicationUserManager>();
                var userByEmail = await UserManager.FindByEmailAsync(model.email);

                if (userByEmail == null)
                {
                    var user = new ApplicationUser()
                    {
                        UserName = model.email, Email = model.email
                    };
                    if (model.menu.Count > 0 || model.groups.Count > 0)
                    {
                        user.PhoneNumberConfirmed = true;
                    }

                    IdentityResult result = await UserManager.CreateAsync(user, model.password);

                    if (!result.Succeeded)
                    {
                        ErrorModel _errors = new ErrorModel();
                        foreach (string error in result.Errors)
                        {
                            _errors.message += error;
                        }
                        throw new Exception(_errors.message);
                    }
                    else
                    {
                        using (TransactionScope scope = new TransactionScope(TransactionScopeAsyncFlowOption.Enabled))
                        {
                            using (SqlConnection connection1 = new SqlConnection(connetionString))
                            {
                                idUser.id = user.Id;
                                model.id  = user.Id;
                                await connection1.OpenAsync();
                                await SaveUser(connection1, model);

                                if (model.isUserAdmin == 0)
                                {
                                    foreach (var item in model.menu)
                                    {
                                        await SaveUsersMenu(connection1, model.id, item);
                                    }
                                    foreach (var item in model.groups)
                                    {
                                        await SaveUsersGroups(connection1, model.id, item);
                                    }
                                }
                                else if (isAdmin && idAdminrole != null && idAdminrole != "")
                                {
                                    await AddAdminRole(connection1, model.id, idAdminrole);
                                }
                                scope.Complete();
                                identitydbContextTransaction.Commit();
                                return(idUser);
                            }
                        }
                    }
                }
                else
                {
                    throw new Exception("Correo ya existe");
                }
            }
        }