Diplom_O/DataBase/AccessDB.cs

226 lines
4.2 KiB
C#
Raw Permalink Normal View History

2024-09-20 08:53:52 +05:00
using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using System.ComponentModel.DataAnnotations.Schema;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;
using System.Threading.Tasks;
namespace Diplom_O.DataBase
{
public class Access
{
[Key, DatabaseGenerated(DatabaseGeneratedOption.Identity)]
public int Id { get; set; }
[ForeignKey("User")]
public int UserId { get; set; }
public User User { get; set; }
public int FormId { get; set; }
public int FormatAccess { get; set; }
}
public static partial class FuncDB
{
public static void AccessCheckValid(Access obj)
{
try
{
if (obj == null) throw new Exception("Ошибка инициализации.");
if (UserGetById(obj.UserId) == null) throw new Exception("Пользователь не указан.");
if (obj.FormId < 0 || obj.FormId >= FormArray.formName.Length) throw new Exception("Неверно указана форма.");
if (obj.FormId < 0 || obj.FormatAccess >= FormArray.formAccess.Length) throw new Exception("Неверно указаны разрешения");
}
catch { throw; }
}
public static Access AccessGetById(int id)
{
try
{
using (var db = new MainDB())
{
var res =
from a in db.Accessy
where a.Id == id
select a;
try
{
return res.Single();
}
catch
{
return null;
}
}
}
catch
{
throw;
}
}
public static Access[] AccessGetByUserIdFormId(int UserId, int FormId)
{
try
{
using (var db = new MainDB())
{
var res =
from a in db.Accessy
where a.UserId == UserId && a.FormId == FormId
select a;
try
{
return res.ToArray();
}
catch
{
throw;
}
}
}
catch
{
throw;
}
}
public static Access AccessGetByUserIdFormIdAccessId(int UserId, int FormId, int AccessId)
{
try
{
using (var db = new MainDB())
{
var res =
from a in db.Accessy
where
a.UserId == UserId &&
a.FormId == FormId &&
a.FormatAccess == AccessId
select a;
try
{
return res.Single();
}
catch
{
return null;
}
}
}
catch
{
throw;
}
}
public static void AccessAdd(Access obj)
{
try
{
AccessCheckValid(obj);
var local_obj = AccessGetByUserIdFormIdAccessId(obj.UserId, obj.FormId, obj.FormatAccess);
if (local_obj == null)
using (var db = new MainDB())
{
db.Accessy.Add(obj);
db.SaveChanges();
}
}
catch
{
throw;
}
}
public static void AccessDelete(Access obj)
{
try
{
if (obj == null)
return;
var localObj = AccessGetById(obj.Id);
if (localObj == null)
throw new Exception("Разрешение не существует.");
using (var db = new MainDB())
{
db.Accessy.Remove(localObj);
db.SaveChanges();
}
}
catch
{
throw;
}
}
public static Access[] AccessList(int UserId)
{
try
{
using (var db = new MainDB())
{
var access = (
from a in db.Accessy
where a.UserId == UserId
orderby a.FormId
orderby a.FormatAccess
select a
).ToArray();
return access;
}
}
catch
{
throw;
}
}
public static bool[,] AccessListTable(int UserId)
{
try
{
var result = new bool[FormArray.formName.Length, FormArray.formAccess.Length];
using (var db = new MainDB())
{
var list = AccessList(UserId);
foreach (var r in list)
result[r.FormId, r.FormatAccess] = true;
return result;
}
}
catch
{
throw;
}
}
public static int AccessToSettingsFullCountWithoutUserId(User user)
{
try
{
using (var db = new MainDB())
{
var users =
(from a in db.Usery
where a.Id != user.Id
select a).ToArray();
var count = 0;
foreach(var usr in users)
{
var access =
from a in db.Accessy
where
a.UserId == usr.Id &&
a.FormId == 7
select a;
if (access.Count() == FormArray.formAccess.Length)
count++;
}
return count;
}
}
catch
{
throw;
}
}
}
}