demo20230512/Models/ExtensionMethods/IdentityExtensions.cs

75 lines
3.3 KiB
C#
Raw Normal View History

2023-05-12 10:20:28 +08:00
using Qcarbon.Database.adminCheck;
using Qcarbon.Interfaces.adminCheck;
using Qcarbon.ViewModels.admin;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Security.Claims;
using System.Security.Principal;
using System.Web;
using System.Web.Configuration;
using Weee.DAL;
using Weee.Service;
namespace Weee.Models.ExtensionMethods
{
public static class IdentityExtensions
{
public static bool IsCompanyAdmin(this IIdentity identity, WeeeDataContext _db)
{
bool ret=false;
var userName = ((ClaimsIdentity)identity).Name;
var qry = (from u in _db.Users
where u.UserName == userName
select u.IsCompanyAdmin).FirstOrDefault();
if (qry != null)
ret = qry;
return ret;
}
public static ACCOUNT_TYPE GetUserAccountType(this IIdentity identity, WeeeDataContext _db)
{
var userName = ((ClaimsIdentity)identity).Name;
var userTypeInfo0 = (from u in _db.Users
join ut in _db.UserAccountType on u.Id equals ut.UserId
into UserT from utt in UserT.DefaultIfEmpty()
where u.UserName.Trim().ToLower() ==
(userName != null && userName.Trim() != "" ?
userName.Trim().ToLower() : "")
&& utt.Enabled == true
select utt).AsQueryable();// .FirstOrDefault();
var userTypeInfo = userTypeInfo0.FirstOrDefault();
ACCOUNT_TYPE ret = userTypeInfo != null ?
userTypeInfo.AccountType : ACCOUNT_TYPE.UNDEFINED; // 1: 僅組織型, 2: 完整功能
if (userTypeInfo == null)
return ret;
var qry = (from a in _db.Users
where a.UserName.ToLower() == userName.ToLower()
select a).FirstOrDefault();
if (qry != null)
{
IcompanyAdminCheckService _companyAdminCheckService =
new companyAdminCheckService(_db, userTypeInfo.UserId);
IadminCheckService _adminCheckService = new adminCheckService(_db
, userTypeInfo.UserId);
if (!_adminCheckService.adminCheckPass())
{
int companyId = _companyAdminCheckService.userId2companyID(userTypeInfo.UserId);
companyAdminAccessVM uaa = _companyAdminCheckService.getCompanyAdminAccessVM(companyId);
if (uaa != null)
ret = uaa.AccountType;
}
}
//string OrganizationOnly = WebConfigurationManager.AppSettings["OrganizationOnly"]; // true or else
//bool isOrganizationOnly;
//if (!bool.TryParse(OrganizationOnly, out isOrganizationOnly))
// isOrganizationOnly = false;
//int ret;
//if (isOrganizationOnly)
// ret = 1;
//else
// ret = userTypeInfo != null ? userTypeInfo.AccountType : 0; // 1: 僅組織型, 2: 完整功能
//return ret;
return ret;
}
}
}