Статусы пользователя + проверка статуса пользователя в AddJWTAuthentication

This commit is contained in:
Olga Nemt 2023-07-12 12:07:56 +05:00
parent 0fbc54e715
commit 9d9ccced4e
7 changed files with 8332 additions and 6 deletions

View File

@ -63,7 +63,7 @@ namespace AsbCloudApp.Data.User
/// <summary>
/// Id состояния пользователя
/// </summary>
public short? IdState { get; set; }
public short IdState { get; set; }
/// <summary>
/// DTO компании

File diff suppressed because it is too large Load Diff

View File

@ -0,0 +1,72 @@
using Microsoft.EntityFrameworkCore.Migrations;
using System;
#nullable disable
namespace AsbCloudDb.Migrations
{
public partial class Update_IdState_For_User : Migration
{
protected override void Up(MigrationBuilder migrationBuilder)
{
migrationBuilder.UpdateData(
table: "t_user",
keyColumn: "state",
keyValue: null,
column: "state",
value: (short)1);
migrationBuilder.AlterColumn<short>(
name: "state",
table: "t_user",
type: "smallint",
nullable: false,
defaultValue: (short)0,
comment: "состояние:\n0 - не активен, \n1 - активен, \n2 - заблокирован",
oldClrType: typeof(short),
oldType: "smallint",
oldNullable: true,
oldComment: "состояние:\n100 - удален");
migrationBuilder.AlterColumn<int>(
name: "id_category",
table: "t_help_page",
type: "integer",
nullable: false,
comment: "Id категории файла",
oldClrType: typeof(int),
oldType: "integer",
oldComment: "id категории файла");
}
protected override void Down(MigrationBuilder migrationBuilder)
{
migrationBuilder.AlterColumn<short>(
name: "state",
table: "t_user",
type: "smallint",
nullable: true,
comment: "состояние:\n100 - удален",
oldClrType: typeof(short),
oldType: "smallint",
oldComment: "состояние:\n0 - не активен, \n1 - активен, \n2 - заблокирован");
migrationBuilder.AlterColumn<int>(
name: "id_category",
table: "t_help_page",
type: "integer",
nullable: false,
comment: "id категории файла",
oldClrType: typeof(int),
oldType: "integer",
oldComment: "Id категории файла");
migrationBuilder.UpdateData(
table: "t_user",
keyColumn: "id",
keyValue: 1,
column: "state",
value: null);
}
}
}

View File

@ -4761,10 +4761,10 @@ namespace AsbCloudDb.Migrations
.HasColumnType("integer")
.HasColumnName("id_company");
b.Property<short?>("IdState")
b.Property<short>("IdState")
.HasColumnType("smallint")
.HasColumnName("state")
.HasComment("состояние:\n100 - удален");
.HasComment("состояние:\n0 - не активен, \n1 - активен, \n2 - заблокирован");
b.Property<string>("Login")
.IsRequired()
@ -4826,6 +4826,7 @@ namespace AsbCloudDb.Migrations
Id = 1,
Email = "",
IdCompany = 1,
IdState = (short)1,
Login = "dev",
Name = "Разработчик",
PasswordHash = "Vlcj|4fa529103dde7ff72cfe76185f344d4aa87931f8e1b2044e8a7739947c3d18923464eaad93843e4f809c5e126d013072"

View File

@ -9,6 +9,7 @@
Login = "dev",
PasswordHash = "Vlcj|4fa529103dde7ff72cfe76185f344d4aa87931f8e1b2044e8a7739947c3d18923464eaad93843e4f809c5e126d013072",
Name = "Разработчик",
IdState = 1,
},
};
}

View File

@ -8,6 +8,8 @@ namespace AsbCloudDb.Model
[Table("t_user"), Comment("Пользователи облака")]
public partial class User : IId
{
public const int ActiveStateId = 1;
[Key]
[Column("id")]
public int Id { get; set; }
@ -23,8 +25,8 @@ namespace AsbCloudDb.Model
[StringLength(255)]
public string PasswordHash { get; set; } = null!;
[Column("state"), Comment("состояние:\n100 - удален")]
public short? IdState { get; set; }
[Column("state"), Comment("состояние:\n0 - не активен, \n1 - активен, \n2 - заблокирован")]
public short IdState { get; set; }
[Column("name"), Comment("имя")]
[StringLength(255)]

View File

@ -1,6 +1,9 @@
using AsbCloudApp.Data.GTR;
using AsbCloudApp.Repositories;
using AsbCloudDb.Model;
using AsbCloudInfrastructure.Services;
using Microsoft.AspNetCore.Authentication.JwtBearer;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.IdentityModel.Tokens;
using Microsoft.OpenApi.Models;
@ -19,7 +22,8 @@ namespace AsbCloudWebApi
services.AddSwaggerGen(c =>
{
c.MapType<DateOnly>(() => new OpenApiSchema { Type = "string", Format = "date" });
c.MapType<JsonValue>(() => new OpenApiSchema {
c.MapType<JsonValue>(() => new OpenApiSchema
{
AnyOf = new OpenApiSchema[]
{
new OpenApiSchema {Type = "string", Format = "string" },
@ -98,6 +102,26 @@ namespace AsbCloudWebApi
context.Token = accessToken;
}
return Task.CompletedTask;
},
OnTokenValidated = context =>
{
var idUser = context.Principal?.GetUserId();
if (idUser is null)
{
context.Fail("idUser is null");
return Task.CompletedTask;
}
context.HttpContext.RequestServices.GetRequiredService<IUserRepository>();
var userService = services.BuildServiceProvider().GetRequiredService<IUserRepository>();
var user = userService.GetOrDefault(idUser.Value);
if (user is null || user.IdState != User.ActiveStateId)
{
context.Fail("invaild user data");
}
return Task.CompletedTask;
}
};