Example #1
0
        // To protect from overposting attacks, please enable the specific properties you want to bind to, for
        // more details see https://aka.ms/RazorPagesCRUD.
        public async Task <IActionResult> OnPostAsync()
        {
            if (!ModelState.IsValid)
            {
                return(Page());
            }
            Student.TenantId = _tenantResolver.GetTenantId();

            _context.Students.Add(Student);
            await _context.SaveChangesAsync();

            return(RedirectToPage("./Index"));
        }
        private async Task SetTenantId(EntityModelCommand <TEntityModel, TResponse> request)
        {
            if (!(request.Model is IHaveTenant <TKey> tenantModel))
            {
                return;
            }

            if (!Equals(tenantModel.TenantId, default(TKey)))
            {
                return;
            }

            var tenantId = await _tenantResolver.GetTenantId(request.Principal);

            tenantModel.TenantId = tenantId;
        }
    private async Task Authorize(EntityModelCommand <TEntityModel, TResponse> request)
    {
        var principal = request.Principal;

        if (principal == null)
        {
            return;
        }

        // check principal tenant is same as model tenant
        if (request.Model is not IHaveTenant <TKey> tenantModel)
        {
            return;
        }

        var tenantId = await _tenantResolver.GetTenantId(principal);

        if (Equals(tenantId, tenantModel.TenantId))
        {
            return;
        }

        throw new DomainException(HttpStatusCode.Forbidden, "User does not have access to specified tenant.");
    }
Example #4
0
 public UsuarioController(UsuarioApp app, UserManager <ApplicationUser> userManager, ITenantResolver tenantResolver)
 {
     _app         = app;
     _userManager = userManager;
     _tenantId    = tenantResolver.GetTenantId();
 }
Example #5
0
        protected override void OnModelCreating(ModelBuilder modelBuilder)
        {
            //foreach (var type in GetEntityTypes())
            //{

            //    var method = SetGlobalQueryMethod.MakeGenericMethod(type);
            //    method.Invoke(this, new object[] { modelBuilder });
            //}

            //this works! that's set the global filter to a LINQ which is a function with a boolean return.
            //  so we can control the LINQ function such as deal with diffrent TenantId to switch Tenant
            //  without change the filter itself.!!!
            // great job!


            //***how to turn off the query filter?!!
            //query filter set when dbConext created, toggle it when login/register etc otherwise it will failed because
            //  of query filter works( this is done by a smart way which turn off the filter when TenantID == Guid.Empty!

            //modelBuilder.Entity<AppUser>().HasQueryFilter(b => (EF.Property<Guid>(b, "TenantId") == _tenantResolver.GetTenantId()) || _bEnableFilter);
            //modelBuilder.Entity<Tenant>().HasQueryFilter(b => (EF.Property<Guid>(b, "TenantId") == _tenantResolver.GetTenantId()) || _bEnableFilter);
            modelBuilder.Entity <AppUser>().HasQueryFilter(b => (EF.Property <Guid>(b, "TenantId") == _tenantResolver.GetTenantId()) || (_tenantResolver.GetTenantId() == Guid.Empty));
            modelBuilder.Entity <Tenant>().HasQueryFilter(b => (EF.Property <Guid>(b, "TenantId") == _tenantResolver.GetTenantId()) || (_tenantResolver.GetTenantId() == Guid.Empty));
            modelBuilder.Entity <Staff>().HasQueryFilter(b => (EF.Property <Guid>(b, "TenantId") == _tenantResolver.GetTenantId()) || (_tenantResolver.GetTenantId() == Guid.Empty));
            //modelBuilder.Entity<IdentityUser>().HasQueryFilter(b => true);
            //modelBuilder.Entity<IdentityUser>().HasQueryFilter(
            //{
            //      {
            //          EF.Property<string>(b, "UserName") == _tenantResolver.CurrentTenant()
            //      }
            //);

            //TenantId+NickName should be unique,basically we can use NickName to find the
            // Student specification.
            //modelBuilder.Entity<Student>().HasIndex(c => new { c.TenantId, c.NickName })
            //    .IsUnique();

            //Set composite primary key
            modelBuilder.Entity <CourseAssignment>()
            .HasKey(c => new { c.CourseId, c.StaffId });

            //Set CourseId,Id unique
            modelBuilder.Entity <Enrollment>()
            .HasKey(e => new { e.CourseId, e.StudentId });
            //.HasIndex(e => new { e.CourseId, e.Id })
            //.IsUnique();

            //Set composite primary key
            modelBuilder.Entity <GuardianRelation>()
            .HasKey(g => new { g.AppUserId, g.Id });

            //.HasKey(g => new { g.Id, g.AppUserId });
            //.HasNoKey()
            //.HasIndex(g => new { g.AppUserId, g.Id });
            //.HasKey(g => new { g.AppUserId, g.Id });
            //Introducing FOREIGN KEY constraint 'FK_GuardianRelation_Student_Id'
            //on table 'GuardianRelation' may cause cycles or multiple cascade paths. Specify ON DELETE NO ACTION or ON UPDAT
            // todo make sure:  **don't know why, so just set Index, not sure about the cascade delete is OK

            base.OnModelCreating(modelBuilder);
        }
Example #6
0
 public BloodContext(ITenantResolver tenantResolver, IUserResolver userResolver) : base()
 {
     this.TenantId = tenantResolver.GetTenantId();
     _userResolver = userResolver;
 }