diff --git a/docs/README.md b/docs/README.md index 595d23d..9b12c36 100644 --- a/docs/README.md +++ b/docs/README.md @@ -4,7 +4,10 @@ - [x] Installation - [x] Database usage - [x] Authorization usage -- [ ] LogicResult usage -- [ ] Repositories usage -- [ ] AuthService usage +- [x] LogicResult usage +- [x] Repositories usage +- [x] AuthService usage +- [x] AuthMiddleware usage - [ ] AdminPages usage +- [ ] Endpoints usage +- [ ] Blazor pages usage diff --git a/docs/api/endpoints.md b/docs/api/endpoints.md new file mode 100644 index 0000000..e69de29 diff --git a/docs/api/installation.md b/docs/api/installation.md index 249bcd7..66021eb 100644 --- a/docs/api/installation.md +++ b/docs/api/installation.md @@ -1,5 +1,5 @@ # Ho to use the Web API version -This Installation adds all HopFrame [endpoints](./endpoints.md) and [services](./services.md) to the application. +This Installation adds all HopFrame [endpoints](./endpoints.md) and [repositories](../repositories.md) to the application. 1. Add the HopFrame.Api library to your project: diff --git a/docs/api/logicresults.md b/docs/api/logicresults.md new file mode 100644 index 0000000..aacb6e5 --- /dev/null +++ b/docs/api/logicresults.md @@ -0,0 +1,33 @@ +# LogicResults +LogicResults provide another layer of abstraction above the ActionResults. +They help you sending the right `HttpStatusCode` with the right data. + +## Usage +1. Create an endpoint that returns an `ActionResult`: + + ```csharp + [HttpGet("hello")] + public ActionResult Hello() { + return new ActionResult("Hello, World!"); + } + ``` +2. Now instead of directly returning the `ActionResult`, return a `LogicResult` + + ```csharp + [HttpGet("hello")] + public ActionResult Hello() { + return LogicResult.Ok("Hello, World!"); + } + ``` +3. This allows you to very easily change the return type by simply calling the right function + + ```csharp + [HttpGet("hello")] + public ActionResult Hello() { + if (!Auth.IsLoggedIn) + return LogicResult.Forbidden(); + + return LogicResult.Ok("Hello, World!"); + } + ``` + **Hint:** You can also provide an error message for status codes that are not in the 200 range. \ No newline at end of file diff --git a/docs/blazor/admin.md b/docs/blazor/admin.md new file mode 100644 index 0000000..4b90907 --- /dev/null +++ b/docs/blazor/admin.md @@ -0,0 +1,2 @@ +# HopFrame Admin Pages + diff --git a/docs/blazor/auth.md b/docs/blazor/auth.md new file mode 100644 index 0000000..0b15e1c --- /dev/null +++ b/docs/blazor/auth.md @@ -0,0 +1,20 @@ +# Auth Service +The `IAuthService` provides some useful methods to handle user authentication (login/register). + +## Usage +Simply define the `IAuthService` as a dependency + +```csharp +public interface IAuthService { + Task Register(UserRegister register); + Task Login(UserLogin login); + Task Logout(); + + Task RefreshLogin(); + Task IsLoggedIn(); +} +``` +## Automatically refresh user sessions +1. Make sure you have implementented the `AuthMiddleware` how it's described in step 5 of the [installation](./installation.md). + +2. After that, the access token of the user gets automatically refreshed as long as the refresh token is valid. diff --git a/docs/blazor/installation.md b/docs/blazor/installation.md index 91e87d7..f740b7b 100644 --- a/docs/blazor/installation.md +++ b/docs/blazor/installation.md @@ -1,5 +1,5 @@ ## How to use the Blazor API -This Installation adds all HopFrame [pages](./pages.md) and [services](./services.md) to the application. +This Installation adds all HopFrame [pages](./pages.md) and [repositories](../repositories.md) to the application. 1. Add the HopFrame.Web library to your project diff --git a/docs/blazor/pages.md b/docs/blazor/pages.md new file mode 100644 index 0000000..e69de29 diff --git a/docs/repositories.md b/docs/repositories.md new file mode 100644 index 0000000..25cb4ac --- /dev/null +++ b/docs/repositories.md @@ -0,0 +1,75 @@ +# HopFrame Repositories +The HopFrame provies repositories for the various build in database models as an abstraction around the `HopDbContext` to ensure, that the data is proccessed and saved correctly. + +## Overview +The repositories can also be used by simply defining them as a dependency in your service / controller. + +### User Repository + +```csharp +public interface IUserRepository { + Task> GetUsers(); + + Task GetUser(Guid userId); + + Task GetUserByEmail(string email); + + Task GetUserByUsername(string username); + + Task AddUser(User user); + + Task UpdateUser(User user); + + Task DeleteUser(User user); + + Task CheckUserPassword(User user, string password); + + Task ChangePassword(User user, string password); +} +``` + +### Group Repository + +```csharp +public interface IGroupRepository { + Task> GetPermissionGroups(); + + Task> GetDefaultGroups(); + + Task> GetUserGroups(User user); + + Task GetPermissionGroup(string name); + + Task EditPermissionGroup(PermissionGroup group); + + Task CreatePermissionGroup(PermissionGroup group); + + Task DeletePermissionGroup(PermissionGroup group); +} +``` + +### Permission Repository + +```csharp +public interface IPermissionRepository { + Task HasPermission(IPermissionOwner owner, params string[] permissions); + + Task AddPermission(IPermissionOwner owner, string permission); + + Task RemovePermission(IPermissionOwner owner, string permission); + + Task> GetFullPermissions(IPermissionOwner owner); +} +``` + +### Token Repository + +```csharp +public interface ITokenRepository { + Task GetToken(string content); + + Task CreateToken(int type, User owner); + + Task DeleteUserTokens(User owner); +} +```