@universis/janitor 2.4.4 → 2.5.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/dist/index.d.ts CHANGED
@@ -357,5 +357,10 @@ declare class DisableNetworkRestrictAccess extends NetworkRestrictAccess {
357
357
 
358
358
  declare function validateNetworkAccess(): Handler;
359
359
 
360
- export { AppRateLimitService, AppSpeedLimitService, DefaultScopeAccessConfiguration, DisableNetworkRestrictAccess, DisableUserClientScopeVerification, EnableNetworkRestrictAccess, EnableScopeAccessConfiguration, ExtendScopeAccessConfiguration, HttpBearerStrategy, HttpRemoteAddrForbiddenError, InvalidClientScopeError, NetworkRestrictAccess, OAuth2ClientService, OutputCachingService, PassportService, RateLimitService, RedisClientStore, RemoteAddressValidator, ScopeAccessConfiguration, ScopeString, SpeedLimitService, UserWithNetworkAddress, VerifyUserClientScope, outputCache, validateClientScope, validateNetworkAccess, validateScope };
360
+ declare class EnableReadAccessScope extends ApplicationService {
361
+ readonly clientScopes: string[];
362
+ protected enable(clientScope: string): void;
363
+ }
364
+
365
+ export { AppRateLimitService, AppSpeedLimitService, DefaultScopeAccessConfiguration, DisableNetworkRestrictAccess, DisableUserClientScopeVerification, EnableNetworkRestrictAccess, EnableReadAccessScope, EnableScopeAccessConfiguration, ExtendScopeAccessConfiguration, HttpBearerStrategy, HttpRemoteAddrForbiddenError, InvalidClientScopeError, NetworkRestrictAccess, OAuth2ClientService, OutputCachingService, PassportService, RateLimitService, RedisClientStore, RemoteAddressValidator, ScopeAccessConfiguration, ScopeString, SpeedLimitService, UserWithNetworkAddress, VerifyUserClientScope, outputCache, validateClientScope, validateNetworkAccess, validateScope };
361
366
  export type { GenericUser, OAuth2AuthorizeUser, OAuth2MethodOptions, OAuth2ServiceSettings, OAuth2User, OAuth2UserProfile, OutputCachingElement, OutputCachingProfile, RateLimitServiceConfiguration, RateLimitStoreConstructor, ScopeAccessConfigurationElement, ScopeAccessConfigurationSection, SpeedLimitServiceConfiguration, SpeedLimitStoreConstructor, UniversisConfigurationSection };
package/dist/index.esm.js CHANGED
@@ -1929,5 +1929,131 @@ class DisableNetworkRestrictAccess extends NetworkRestrictAccess {
1929
1929
  }
1930
1930
  }
1931
1931
 
1932
- export { AppRateLimitService, AppSpeedLimitService, DefaultScopeAccessConfiguration, DisableNetworkRestrictAccess, DisableUserClientScopeVerification, EnableNetworkRestrictAccess, EnableScopeAccessConfiguration, ExtendScopeAccessConfiguration, HttpAccountDisabled, HttpBearerStrategy, HttpBearerTokenExpired, HttpBearerTokenNotFound, HttpBearerTokenRequired, HttpNetworkAccessForbiddenError, HttpRemoteAddrForbiddenError, InvalidClientScopeError, NetworkRestrictAccess, OAuth2ClientService, OutputCachingService, PassportService, RateLimitService, RedisClientStore, RemoteAddressValidator, ScopeAccessConfiguration, ScopeString, SpeedLimitService, UserWithNetworkAddress, VerifyUserClientScope, outputCache, validateClientScope, validateNetworkAddress, validateScope };
1932
+ class EnableReadAccessScope extends ApplicationService {
1933
+ /**
1934
+ *
1935
+ * @param {import('@themost/express').ExpressDataApplication|import('@themost/common').ApplicationBase} app
1936
+ */
1937
+ constructor(app) {
1938
+ super(app);
1939
+ /**
1940
+ * @type {import('rxjs').Observable<import('express').Application>}
1941
+ */
1942
+ const container = app && app.container;
1943
+ if (container) {
1944
+ container.subscribe((container) => {
1945
+ if (container) {
1946
+ for (const scope of this.clientScopes) {
1947
+ this.enable(scope);
1948
+ }
1949
+ }
1950
+ });
1951
+ }
1952
+ }
1953
+
1954
+ get clientScopes() {
1955
+ return [
1956
+ 'students',
1957
+ 'teachers',
1958
+ 'registrar'];
1959
+
1960
+ }
1961
+
1962
+ /**
1963
+ * @protected
1964
+ * @param {string} scope
1965
+ */
1966
+ enable(scope) {
1967
+ const searchScope = scope;
1968
+ const targetScope = scope + ':read';
1969
+ // get schema loader strategy for enumerating models
1970
+ const schemaLoader = this.getApplication().getConfiguration().getStrategy(SchemaLoaderStrategy);
1971
+ if (schemaLoader) {
1972
+ const models = schemaLoader.getModels();
1973
+ let updated = 0;
1974
+ // enumerate models that includes scope based privileges
1975
+ // where mask is 1 (read)
1976
+ for (const model of models) {
1977
+ /**
1978
+ * @type {import('@themost/common').DataModelProperties}
1979
+ */
1980
+ const definition = schemaLoader.getModelDefinition(model);
1981
+ if (Array.isArray(definition.privileges)) {
1982
+ const privileges = definition.privileges.filter((item) => {
1983
+ return item.mask === 1 && Array.isArray(item.scope) && item.scope.includes(searchScope);
1984
+ });
1985
+ let shouldUpdate = false;
1986
+ if (privileges.length > 0) {
1987
+ shouldUpdate = true;
1988
+ // add target scope to privileges
1989
+ for (const privilege of privileges) {
1990
+ if (!privilege.scope.includes(targetScope)) {
1991
+ privilege.scope.push(targetScope);
1992
+ }
1993
+ }
1994
+ }
1995
+ // get fields that includes scope based privileges
1996
+ const fields = definition.fields.filter((item) => {
1997
+ return Array.isArray(item.privileges);
1998
+ }).filter((item) => {
1999
+ const privileges = item.privileges.filter((privilege) => {
2000
+ return privilege.mask === 1 && Array.isArray(privilege.scope) && privilege.scope.includes(searchScope);
2001
+ });
2002
+ return privileges.length > 0;
2003
+ });
2004
+ if (fields.length > 0) {
2005
+ shouldUpdate = true;
2006
+ for (const field of fields) {
2007
+ const privileges = field.privileges.filter((privilege) => {
2008
+ return privilege.mask === 1 && Array.isArray(privilege.scope) && privilege.scope.includes(searchScope);
2009
+ });
2010
+ for (const privilege of privileges) {
2011
+ if (!privilege.scope.includes(targetScope)) {
2012
+ privilege.scope.push(targetScope);
2013
+ }
2014
+ }
2015
+ }
2016
+ }
2017
+ if (shouldUpdate) {
2018
+ updated++;
2019
+ // update model definition
2020
+ schemaLoader.setModelDefinition(definition);
2021
+ }
2022
+ }
2023
+ }
2024
+ if (updated > 0) {
2025
+ TraceUtils.log(`${this.constructor.name}: Updated ${updated} model definition(s) to include scope "${targetScope}"`);
2026
+ }
2027
+ }
2028
+
2029
+ const scopeConfiguration = this.getApplication().getConfiguration().getStrategy(ScopeAccessConfiguration);
2030
+ if (scopeConfiguration) {
2031
+ const elements = scopeConfiguration.elements.
2032
+ filter((item) => item.scope.includes(searchScope)).
2033
+ filter((item) => item.access.includes('read'));
2034
+ if (elements.length > 0) {
2035
+ const insertElements = [];
2036
+ for (const element of elements) {
2037
+ insertElements.push({
2038
+ insertAfter: element,
2039
+ resource: element.resource,
2040
+ scope: [targetScope],
2041
+ access: ['read']
2042
+ });
2043
+ }
2044
+ for (const { insertAfter, resource, scope, access } of insertElements) {
2045
+ const index = scopeConfiguration.elements.findIndex((element) => element === insertAfter);
2046
+ scopeConfiguration.elements.splice(index + 1, 0, {
2047
+ resource,
2048
+ scope,
2049
+ access
2050
+ });
2051
+ }
2052
+ TraceUtils.log(`${this.constructor.name}: Updated ${elements.length} scope access configuration(s) to include scope "${targetScope}"`);
2053
+ }
2054
+ }
2055
+ }
2056
+ }
2057
+
2058
+ export { AppRateLimitService, AppSpeedLimitService, DefaultScopeAccessConfiguration, DisableNetworkRestrictAccess, DisableUserClientScopeVerification, EnableNetworkRestrictAccess, EnableReadAccessScope, EnableScopeAccessConfiguration, ExtendScopeAccessConfiguration, HttpAccountDisabled, HttpBearerStrategy, HttpBearerTokenExpired, HttpBearerTokenNotFound, HttpBearerTokenRequired, HttpNetworkAccessForbiddenError, HttpRemoteAddrForbiddenError, InvalidClientScopeError, NetworkRestrictAccess, OAuth2ClientService, OutputCachingService, PassportService, RateLimitService, RedisClientStore, RemoteAddressValidator, ScopeAccessConfiguration, ScopeString, SpeedLimitService, UserWithNetworkAddress, VerifyUserClientScope, outputCache, validateClientScope, validateNetworkAddress, validateScope };
1933
2059
  //# sourceMappingURL=index.esm.js.map