Пример #1
0
        /// <summary>
        /// Create roles and return the admin role id
        /// </summary>
        /// <param name="tenantId"></param>
        /// <param name="companyId"></param>
        public string CreateTenantRole(string tenantId, string companyId)
        {
            var roles = StaticRoles.GetRoles();

            if (_roleRepository.GetAll().FilterTenant(tenantId).Any())
            {
                return(null);
            }

            string adminRoleId = Guid.NewGuid().ToString();

            foreach (var role in roles)
            {
                var id = role.Name == StaticRoles.Admin.Name ? adminRoleId : Guid.NewGuid().ToString();
                _roleRepository.Create(new Role
                {
                    Id          = id,
                    Name        = role.Name,
                    DisplayName = role.DisplayName,
                    AccessLevel = role.AccessLevel,
                    TenantId    = tenantId,
                    CompanyId   = companyId
                });
            }

            _roleRepository.Save();

            return(adminRoleId);
        }
        public virtual TEntity Create(TEntity entity)
        {
            Repository.Create(entity);
            Repository.Save();

            return(entity);
        }
        public bool AddList(List <Permission> models)
        {
            List <Permission> missingModels = new List <Permission>();

            var resources = _resourceRepository.GetAllIgnoreFilter().Where(x => x.IsPublic == false).ToList();

            foreach (var model in models)
            {
                var resource       = resources.First(x => x.Id == model.ResourceId);
                var parentResource = resources.FirstOrDefault(x => x.Route == resource.ParentRoute);
                if (parentResource != null && models.All(x => x.ResourceId != parentResource.Id) && missingModels.All(x => x.ResourceId != parentResource.Id))
                {
                    missingModels.Add(new Permission()
                    {
                        Id         = Guid.NewGuid().ToString(),
                        ResourceId = parentResource.Id,
                        RoleId     = model.RoleId,
                        TenantId   = model.TenantId
                    });
                }
            }


            if (missingModels.Any())
            {
                models.AddRange(missingModels);
            }


            var permissions = models.Select(model => _repository.GetAll().Where(x => x.RoleId == model.RoleId).ToList()).FirstOrDefault();

            if (permissions != null)
            {
                _repository.Delete(permissions);
            }

            if (models.Any(model => model.ResourceId != "00000000-0000-0000-0000-000000000000"))
            {
                _repository.Create(models);
            }

            _repository.Save();

            return(true);
        }