# Class AuthorityRepository

Namespace
  adas_core.Infrastructure.Repositories

Assembly
  adas-core.Infrastructure.dll

Repository for managing user authorities and permissions in the system. This repository provides methods to create, retrieve, and delete authorities based on user and unit identifiers. It also includes logging for debugging purposes and ensures that initial data is loaded when necessary.

    public class AuthorityRepository : MongoRepository<Authorization>, IAuthorityRepository, IMongoRepository<Authorization>

Inheritance
  object
  MongoRepository<Authorization>
  AuthorityRepository

Implements
  IAuthorityRepository
  IMongoRepository<Authorization>

Inherited Members
  MongoRepository<Authorization>.Db
  MongoRepository<Authorization>.Collection
  MongoRepository<Authorization>.InsertOneAsync(Authorization)
  MongoRepository<Authorization>.UpdateOneAsync(ObjectId, Authorization)
  MongoRepository<Authorization>.DeleteAsync(ObjectId)
  MongoRepository<Authorization>.CreateIndexes()
  MongoRepository<Authorization>.InsertManyAsync(List<Authorization>)
  MongoRepository<Authorization>.UpdateManyObjectIdAsync(string, ObjectId, ObjectId)
  MongoRepository<Authorization>.DeleteAsync(string)
  MongoRepository<Authorization>.CollectionExists(string)
  object.Equals(object)
  object.Equals(object, object)
  object.GetHashCode()
  object.GetType()
  object.MemberwiseClone()
  object.ReferenceEquals(object, object)
  object.ToString()

Extension Methods
  NumberUtils.IsNumber(object)
  NumberUtils.ToDouble(object)
  ObjectUtils.ToStr(object?)

## Constructors

### AuthorityRepository(IOptions<ApiSettings>, IMongoDatabase, ILogger<AuthorityRepository>)

Repository for managing user authorities and permissions in the system. This repository provides methods to create, retrieve, and delete authorities based on user and unit identifiers. It also includes logging for debugging purposes and ensures that initial data is loaded when necessary.

    public AuthorityRepository(IOptions<ApiSettings> apiSettings, IMongoDatabase database, ILogger<AuthorityRepository> logger)

#### Parameters

`apiSettings` IOptions<ApiSettings>

  The API settings containing configuration for the repository.

`database` IMongoDatabase

  The MongoDB database instance.

`logger` ILogger<AuthorityRepository>

  The logger for logging information, warnings, and errors.

## Methods

### CreateNewAuthority(string, ObjectId)

Creates a new authority for a user with the specified role. This method generates a new display ID for the authority and inserts it into the database. It also logs the creation of the new authority for debugging purposes.

    public void CreateNewAuthority(string roleName, ObjectId userId)

#### Parameters

`roleName` string

  The name of the role to be assigned to the new authority.

`userId` ObjectId

  The ID of the user for whom the new authority is being created.

### DeleteAllAuthoritiesByDisplay(ObjectId)

Deletes all authorities associated with a specific display ID. This method constructs a filter to identify authorities that match the provided display ID and deletes them from the database. It also logs the deletion of authorities for debugging purposes.

    public Task<bool> DeleteAllAuthoritiesByDisplay(ObjectId displayId)

#### Parameters

`displayId` ObjectId

  The ID of the display whose authorities are being deleted.

#### Returns

Task<bool>

  A boolean value indicating whether the deletion was successful.

### DeleteAllAuthoritiesByUnit(ObjectId)

Deletes all authorities associated with a specific unit ID. This method constructs a filter to identify authorities that match the provided unit ID and deletes them from the database. It also logs the deletion of authorities for debugging purposes.

    public Task<bool> DeleteAllAuthoritiesByUnit(ObjectId unitId)

#### Parameters

`unitId` ObjectId

  The ID of the unit whose authorities are being deleted.

#### Returns

Task<bool>

  A boolean value indicating whether the deletion was successful.

### DeleteAllAuthoritiesByUser(ObjectId)

Deletes all authorities associated with a specific user ID. This method constructs a filter to identify authorities that match the provided user ID and deletes them from the database. It also logs the deletion of authorities for debugging purposes.

    public Task<bool> DeleteAllAuthoritiesByUser(ObjectId userId)

#### Parameters

`userId` ObjectId

  The ID of the user whose authorities are being deleted.

#### Returns

Task<bool>

  A boolean value indicating whether the deletion was successful.

### GetAllAuthorities()

Retrieves a list of all authorities in the system. This method queries the database for all authority records and returns them as a list. It also logs the retrieval of all authorities for debugging purposes.

    public Task<List<Authorization>> GetAllAuthorities()

#### Returns

Task<List<Authorization>>

  A list of all authorities in the system.

### GetById(ObjectId)

Retrieves an authority by its unique identifier. This method constructs a filter to identify the authority that matches the provided ID and retrieves it from the database. It also logs the retrieval of the authority for debugging purposes.

    public Task<Authorization> GetById(ObjectId authId)

#### Parameters

`authId` ObjectId

  The unique identifier of the authority to be retrieved.

#### Returns

Task<Authorization>

  The authority that matches the provided ID, or null if no matching authority is found.

### GetByUnitId(ObjectId)

Retrieves a list of authorities associated with a specific unit ID. This method queries the database for authorities that match the provided unit ID and returns them as a list. It also logs the retrieval of authorities for debugging purposes.

    public Task<List<Authorization>> GetByUnitId(ObjectId unitId)

#### Parameters

`unitId` ObjectId

  The ID of the unit whose authorities are being retrieved.

#### Returns

Task<List<Authorization>>

  A list of authorities associated with the specified unit ID.

### GetCollectionName()

Retrieves the name of the collection used for storing authorities in the database. This method returns the collection name as specified in the API settings configuration.

    public override string GetCollectionName()

#### Returns

string

### GetUserAuthorities(ObjectId)

Retrieves a list of authorities associated with a specific user ID. This method queries the database for authorities that match the provided user ID and returns them as a list. It also logs the retrieval of authorities for debugging purposes.

    public Task<List<Authorization>> GetUserAuthorities(ObjectId userId)

#### Parameters

`userId` ObjectId

  The ID of the user whose authorities are being retrieved.

#### Returns

Task<List<Authorization>>

  A list of authorities associated with the specified user ID.

### InsertInitialLoad()

Inserts initial data into the database for authorities. This method checks if a system user exists and if they do, it ensures that they have the necessary panel authorization. If the system user does not have panel authorization, it creates a new authority for them with the appropriate permissions. This method is typically called during the initial setup of the application to ensure that essential data is present in the database.

    public override sealed Task InsertInitialLoad()

#### Returns

Task

  A task representing the asynchronous operation.