Exemplo n.º 1
0
        private async Task <MxReturnCode <Guid> > SeedStdTermsConditions(GdprSeedRepo gdprSeedRepo)
        {
            MxReturnCode <Guid> rc = new MxReturnCode <Guid>("MxIdentityDb.CreateStdTermsConditions()");

            if (gdprSeedRepo == null)
            {
                rc.SetError(3060301, MxError.Source.Param, "gdprSeedRepo is null");
            }
            else
            {
                var resGetExisting = await gdprSeedRepo.GetWstAsync(_config["WST:Standard"]);

                rc += resGetExisting;
                if (rc.IsSuccess())
                {
                    if (resGetExisting.GetResult() != null)
                    {
                        rc.SetResult(resGetExisting.GetResult().Id);
                    }
                    else
                    {
                        var resCreate = await gdprSeedRepo.CreateWstAsync(_config["WST:Standard"], "descr", "url");

                        rc += resCreate;
                        if (rc.IsSuccess())
                        {
                            var resGetNew = await gdprSeedRepo.GetWstAsync(_config["WST:Standard"]);

                            rc += resGetNew;
                            if (rc.IsSuccess())
                            {
                                if (resGetExisting.GetResult() == null)
                                {
                                    rc.SetError(3060302, MxError.Source.Data, @"wst cannot be created",
                                                MxMsgs.MxErrUnexpected);
                                }
                                else
                                {
                                    var wst = resGetExisting.GetResult().Id;
                                    if (wst == Guid.Empty)
                                    {
                                        rc.SetError(3060303, MxError.Source.Sys,
                                                    @"wst Default not found and cannot be created", MxMsgs.MxErrUnexpected);
                                    }
                                    else
                                    {
                                        rc.SetResult(wst);
                                    }
                                }
                            }
                        }
                    }
                }
            }
            return(rc);
        }
Exemplo n.º 2
0
        private async Task <MxReturnCode <bool> > DeseedStdUrdRole(GdprSeedRepo gdprSeedRepo, string urdName, Guid wstId)
        {
            MxReturnCode <bool> rc = new MxReturnCode <bool>($"MxIdentityDb.DeseedStdUrdRole(name={urdName ?? "[null]"})");

            if ((gdprSeedRepo == null) || (string.IsNullOrWhiteSpace(urdName)) || (gdprSeedRepo.GetStdGdprUrdCode(urdName) == UrdCodeStd.Undefined))
            {
                rc.SetError(3060401, MxError.Source.Param, $"gdprSeedRepo is null or urdName={urdName ?? "[null]"} is invalid");
            }
            else
            {
                var resUrdExists = await gdprSeedRepo.IsExistStdUrdAsync(urdName);

                rc += resUrdExists;
                if (rc.IsSuccess())
                {
                    if (resUrdExists.GetResult() == false)
                    {
                        var resCreate = await gdprSeedRepo.CreateStdUrdAsync(urdName, wstId);

                        rc += resCreate;
                    }
                    if (rc.IsSuccess())
                    {
                        var identityRoleName = gdprSeedRepo.XlatUrdNameToIdentityRoleName(urdName);
                        if (await _roleManager.FindByNameAsync(identityRoleName) != null)
                        {
                            rc.SetResult(true);
                        }
                        else
                        {
                            var idres = await _roleManager.CreateAsync(new IdentityRole()
                            {
                                Name = identityRoleName
                            });

                            if (idres.Succeeded)
                            {
                                rc.SetError(3060402, MxError.Source.Sys, $"unable to create Identity Role {identityRoleName ?? "[null]"}");
                            }
                            else
                            {
                                rc.SetResult(true);
                            }
                        }
                    }
                    if (rc.IsError(true))
                    {
                        await gdprSeedRepo.DeleteStdUrdAsync(urdName);
                    }
                }
            }
            return(rc);
        }
Exemplo n.º 3
0
        static async Task <int> Main(string[] args)
        {
            MxReturnCode <int> rc = new MxReturnCode <int>($"{Program.WebAppName} v{Program.WebAppVersion}", 1);

            rc.Init(Assembly.GetExecutingAssembly(), "*****@*****.**", null, null, null, MxMsgs.SupportedCultures);

            Console.WriteLine(rc.GetInvokeDetails());

            var config = new ConfigurationBuilder().SetBasePath(Directory.GetCurrentDirectory())
                         .AddJsonFile("local.settings.json")
                         .Build();
            var conn = config?["ConnectionStrings:DefaultConnection"];  //03-12-18

            if (conn == null)
            {
                rc.SetError(2010101, MxError.Source.AppSetting, "config not built or ConnectionStrings:DefaultConnection not found");
            }
            else
            {
                using (IAdminRepo repo = new AdminRepo(conn))
                {
                    rc += await repo.GetUrdCountAsync();
                }
                if (rc.IsSuccess(true))
                {
                    Console.WriteLine($"Roles found = {rc.GetResult()}");
                    rc.SetResult(0);
                }
            }
            Console.WriteLine(rc.IsError(true) ? rc.GetErrorUserMsg() : $"Hello World!");
            Console.WriteLine(rc.IsError(true) ? rc.GetErrorTechMsg(): "no error");

            return(rc.GetResult());
        }
Exemplo n.º 4
0
        private async Task <MxReturnCode <bool> > DeseedStdTermsConditions(GdprSeedRepo gdprSeedRepo)
        {
            MxReturnCode <bool> rc = new MxReturnCode <bool>($"MxIdentityDb.DeseedStdTermsCondition");

            if (gdprSeedRepo == null)
            {
                rc.SetError(3060301, MxError.Source.Param, "gdprSeedRepo is null");
            }
            else
            {
                var resGetExisting = await gdprSeedRepo.GetWstAsync(_config["WST:Standard"]);

                rc += resGetExisting;
                if (rc.IsSuccess())
                {
                    if (resGetExisting.GetResult() == null)
                    {
                        rc.SetResult(true);
                    }
                    else
                    {
                        //delete resGetExisting.GetResult()
                        rc.SetResult(true);
                    }
                }
            }
            return(rc);
        }
Exemplo n.º 5
0
        public async Task <IActionResult> OnGetAsync(string msgJson)
        {
            MxReturnCode <IActionResult> rc = new MxReturnCode <IActionResult>("Index.OnGetAsync()", Page());

            try
            {
                using (IAdminRepository repository = new AdminRepository(_conn))
                {
                    var resCnt = await repository.GetRoleCountAsync();

                    rc += resCnt;
                    if (rc.IsSuccess())
                    {
                        URDCount = String.Format("URD Count = {0}", resCnt.GetResult());
                        SetPageStatusMsg("Database access ok", ExistingMsg.Keep);
                        rc.SetResult(Page());
                    }
                }
            }
            catch (Exception e)
            {
                rc.SetError(3130101, MxError.Source.Exception, e.Message, MxMsgs.MxErrUnknownException, true);
            }
            if (rc.IsError())
            {
                _logger.LogError(rc.GetErrorTechMsg());
                SetPageStatusMsg(rc.GetErrorUserMsgHtml(Startup.WebAppName, WebErrorHandling.GetMxRcReportToEmailBody()), ExistingMsg.Overwrite);
            }
            return(rc.GetResult());
        }
Exemplo n.º 6
0
        public async Task <MxReturnCode <int> > ResetAsync()
        {
            MxReturnCode <int> rc = new MxReturnCode <int>("MxIdentitySeedDb.ResetAsync()");

            try
            {
                using (var gdprSeedRepo = new GdprSeedRepo(_config?.GetConnectionString("DefaultConnection"))) //could also be GdprSeedRepoDummy
                {
                    var resWst = await DeseedStdTermsConditions(gdprSeedRepo);

                    rc += resWst;
                    if (rc.IsSuccess())
                    {
                        var wst = resWst.GetResult();

                        //var resRole = await SeedStdUrdRole(gdprSeedRepo, AdminRepo.GdprUrdAdmin, wst);
                        //rc += resRole;
                        //if (rc.IsSuccess())
                        //{
                        //    resRole = await SeedStdUrdRole(gdprSeedRepo, AdminRepo.GdprUrdController, wst);
                        //    rc += resRole;
                        //    if (rc.IsSuccess())
                        //    {
                        //        resRole = await SeedStdUrdRole(gdprSeedRepo, AdminRepo.GdprUrdStandard, wst);
                        //        rc += resRole;
                        //        if (rc.IsSuccess())
                        //        {
                        //            resRole = await SeedStdUrdRole(gdprSeedRepo, AdminRepo.GdprUrdSystem, wst);
                        //            rc += resRole;
                        //            if (rc.IsSuccess())
                        //            {
                        //                resRole = await SeedStdUrdRole(gdprSeedRepo, AdminRepo.GdprUrdGuest, wst);
                        //                rc += resRole;
                        //                if (rc.IsSuccess())
                        //                {
                        //                    resRole = await SeedStdUrdRole(gdprSeedRepo, AdminRepo.GdprUrdGhost, wst);
                        //                    rc += resRole;
                        //                    if (rc.IsSuccess())
                        //                    {

                        //                        //create GoldUser - [email protected]

                        //                        rc.SetResult(0);
                        //                    }
                        //                }
                        //            }
                        //        }
                        //    }
                        //}
                        rc.SetResult(0);
                    }
                }
            }
            catch (Exception e)
            {
                rc.SetError(3060201, MxError.Source.Exception, e.Message, MxMsgs.MxErrUnexpected);
            }
            return(rc);
        }
Exemplo n.º 7
0
        static async Task <int> Main(string[] args) //Uses Dapper/Gdpr.Domain Repository classes to access the database
        {
            MxUserMsg.Init(Assembly.GetExecutingAssembly(), MxMsgs.SupportedCultures);

            MxReturnCode <int> rc = new MxReturnCode <int>(string.Format("{0} v{1}", "ReturnCodeApp()", GetVersion()), 1, "*****@*****.**");

            var config = new ConfigurationBuilder().SetBasePath(Directory.GetCurrentDirectory())
                         .AddJsonFile("local.settings.json")
                         .Build();
            var conn = config?["ConnectionStrings:DefaultConnection"];  //03-12-18

            if (conn == null)
            {
                rc.SetError(2010101, MxError.Source.AppSetting, "config not built or ConnectionStrings:DefaultConnection not found");
            }
            else
            {
                using (IAdminRepository repository = new AdminRepository(conn))
                {
                    rc += await repository.GetRoleCountAsync();
                }
                if (rc.IsSuccess())
                {
                    Console.WriteLine(@"URD Count = {0}", rc.GetResult());
                }
            }
            if (rc.IsError())
            {
                Console.WriteLine(rc.GetErrorUserMsg());
                Console.WriteLine(rc.GetErrorTechMsg());
            }
            else
            {
                Console.WriteLine("ends ok");
            }

            return(rc.IsSuccess() ? 0 : -1);
        }
Exemplo n.º 8
0
        static async Task <int> Main(string[] args)  //03-12-18 made async and returns int not void
        {
            MxReturnCode <int> rc = new MxReturnCode <int>("Main()", -1);

            try
            {
                IWebHost host = CreateWebHostBuilder(args).Build();      //calls Startup.ConfigureServices()
                using (var scope = host?.Services?.CreateScope())
                {
                    var services = scope.ServiceProvider;
                    if (services == null)
                    {
                        rc.SetError(3050101, MxError.Source.Sys, "scope.ServiceProvider is null");
                    }
                    else
                    {
                        var mxIdentitySeedDb = services.GetRequiredService <IMxIdentitySeedDb>();
                        if (mxIdentitySeedDb == null)
                        {
                            rc.SetError(3050102, MxError.Source.Sys, "mxIdentityDb is null");
                        }
                        else
                        {
                            rc = await mxIdentitySeedDb.SetupAsync();

                            if (rc.IsSuccess(true))
                            {
                                host.Run();      //calls Startup.Configure()
                                rc.SetResult(0); //success - webapp has completed
                            }
                        }
                    }
                }
            }
            catch (Exception e)
            {
                rc.SetError(3050103, MxError.Source.Exception, e.Message, MxMsgs.MxErrUnknownException, true);
            }
            return(rc.GetResult());
        }
Exemplo n.º 9
0
        public async static Task <MxReturnCode <int> > EnsureDataPresentAsync(IConfiguration config, UserManager <IdentityUser> userManager, RoleManager <IdentityRole> roleManager)
        {
            MxReturnCode <int> rc = new MxReturnCode <int>("SeedDbIdentity.EnsureSeedDataAsync()", -1, "*****@*****.**");

            if ((config == null) || (userManager == null) || (roleManager == null))
            {
                rc.SetError(3150101, MxError.Source.Param, "configuration, userManager or roleManager is null", MxMsgs.MxErrUnexpected);
            }
            else
            {
                try
                {
                    Guid wst       = new Guid();
                    int  recordCnt = 0;
                    var  conn      = config.GetConnectionString("DefaultConnection");
                    using (IControllerRepository ctrlRepo = new ControllerRepository(conn))
                    {           //create standard T&C
                        var resGet = await ctrlRepo.GetTermsConditionsAsync(config["WST:Standard"]);

                        rc += resGet;
                        if ((resGet.IsSuccess()) && (resGet.GetResult() != null))
                        {
                            wst = resGet.GetResult().Id;
                        }
                        else
                        {
                            var resCreate = await ctrlRepo.CreateTermsConditionsAsync(config["WST:Standard"]);

                            rc += resCreate;
                            if (resCreate.IsSuccess())
                            {
                                resGet = await ctrlRepo.GetTermsConditionsAsync(config["WST:Standard"]);

                                if (resGet.IsSuccess())
                                {
                                    if (resGet.GetResult() == null)
                                    {
                                        rc.SetError(3150102, MxError.Source.Data, @"wst cannot be created", MxMsgs.MxErrUnexpected);
                                    }
                                    else
                                    {
                                        wst = resGet.GetResult().Id;
                                        recordCnt++;
                                    }
                                }
                            }
                        }
                    }
                    var tmp = rc.GetErrorCode();
                    if (rc.IsSuccess())
                    {
                        if (wst == Guid.Empty)
                        {
                            rc.SetError(3150103, MxError.Source.Sys, @"wst Default not found and cannot be created", MxMsgs.MxErrUnexpected);
                        }
                        else
                        {
                            using (IAdminRepository adminRepo = new AdminRepository(conn))
                            {           //create Roles 1) Admin 2) Controller  3) Standard 4) Guest 5) System 6) Ghost
                                if (await IdentityDb.IsExistsRole(adminRepo, roleManager, AdminRepository.GdpaRoleAdmin) == false)
                                {
                                    var res = await IdentityDb.CreateRole(adminRepo, roleManager, wst, AdminRepository.GdpaRoleAdmin);

                                    rc += res;
                                    if (res.IsSuccess())
                                    {
                                        recordCnt++;
                                    }
                                }
                                if ((rc.IsSuccess()) && (await IdentityDb.IsExistsRole(adminRepo, roleManager, AdminRepository.GdpaRoleController) == false))
                                {
                                    var res = await IdentityDb.CreateRole(adminRepo, roleManager, wst, AdminRepository.GdpaRoleController);

                                    rc += res;
                                    if (res.IsSuccess())
                                    {
                                        recordCnt++;
                                    }
                                }
                                if ((rc.IsSuccess()) && (await IdentityDb.IsExistsRole(adminRepo, roleManager, AdminRepository.GdpaRoleStandard) == false))
                                {
                                    var res = await IdentityDb.CreateRole(adminRepo, roleManager, wst, AdminRepository.GdpaRoleStandard);

                                    rc += res;
                                    if (res.IsSuccess())
                                    {
                                        recordCnt++;
                                    }
                                }
                                if ((rc.IsSuccess()) && (await IdentityDb.IsExistsRole(adminRepo, roleManager, AdminRepository.GdpaRoleGuest) == false))
                                {
                                    var res = await IdentityDb.CreateRole(adminRepo, roleManager, wst, AdminRepository.GdpaRoleGuest);

                                    rc += res;
                                    if (res.IsSuccess())
                                    {
                                        recordCnt++;
                                    }
                                }
                                if ((rc.IsSuccess()) && (await IdentityDb.IsExistsRole(adminRepo, roleManager, AdminRepository.GdpaRoleSystem) == false))
                                {
                                    var res = await IdentityDb.CreateRole(adminRepo, roleManager, wst, AdminRepository.GdpaRoleSystem);

                                    rc += res;
                                    if (res.IsSuccess())
                                    {
                                        recordCnt++;
                                    }
                                }
                                if ((rc.IsSuccess()) && (await IdentityDb.IsExistsRole(adminRepo, roleManager, AdminRepository.GdpaRoleGhost) == false))
                                {
                                    var res = await IdentityDb.CreateRole(adminRepo, roleManager, wst, AdminRepository.GdpaRoleGhost);

                                    rc += res;
                                    if (res.IsSuccess())
                                    {
                                        recordCnt++;
                                    }
                                }
                            }
                            if (rc.IsSuccess())
                            {
                                using (ISysRepository sysRepo = new SysRepository(conn))
                                {
                                    if (await IdentityDb.IsExistsUser(sysRepo, userManager, config["AdminGold:Email"]) == false)
                                    {   //Create AdminGold user
                                        var res = await IdentityDb.CreateUser(sysRepo, userManager, AdminRepository.GdpaRoleAdmin, config["AdminGold:Password"], config["AdminGold:Email"], config["AdminGold:Fullname"]);

                                        rc += res;
                                        if (res.IsSuccess())
                                        {
                                            recordCnt++;
                                        }
                                    }
                                }
                            }
                            if (rc.IsSuccess())
                            {
                                rc.SetResult(recordCnt);
                            }
                        }
                    }
                }
                catch (Exception e)
                {
                    rc.SetError(3150104, MxError.Source.Exception, e.Message, MxMsgs.MxErrUnknownException, true);
                }
            }
            return(rc);
        }