159 lines
6.0 KiB
C#
159 lines
6.0 KiB
C#
using System;
|
|
using System.Collections.Generic;
|
|
using System.Linq;
|
|
using System.Net;
|
|
using System.Net.Http;
|
|
using System.Web.Http;
|
|
using Microsoft.AspNet.Identity;
|
|
using Weee.DAL;
|
|
using Weee.Filter;
|
|
using Weee.Models;
|
|
using System.Data.Entity;
|
|
using Weee.Service;
|
|
using CScommon;
|
|
using Qcarbon.Interfaces.adminCheck;
|
|
using NLog;
|
|
using System.Web.Http.Controllers;
|
|
using Qcarbon.Database.adminCheck;
|
|
using Qcarbon.ViewModels.admin;
|
|
|
|
namespace Weee.Controllers
|
|
{
|
|
[Authorize(Roles = ProgramConstants.normalcompany)]
|
|
public class UserController : ApiController
|
|
{
|
|
private readonly WeeeDataContext db;
|
|
//private readonly UserManager<User> manager;
|
|
protected IadminCheckService _adminCheckService;
|
|
protected IcompanyAdminCheckService _companyAdminCheckService;
|
|
protected Logger log;
|
|
private UserService userService;
|
|
|
|
public UserController(WeeeDataContext d, UserManager<User> m)
|
|
{
|
|
db = d;
|
|
//manager = m;
|
|
userService = new UserService(d, m);
|
|
}
|
|
protected override void Initialize(HttpControllerContext controllerContext)
|
|
{
|
|
base.Initialize(controllerContext);
|
|
var userid = User.Identity.GetUserId();
|
|
_adminCheckService = new adminCheckService(db, userid);
|
|
_companyAdminCheckService = new companyAdminCheckService(db, userid);
|
|
log = NLog.LogManager.GetCurrentClassLogger();
|
|
}
|
|
|
|
[Route("api/User/GetByMyCompany")]
|
|
[HttpGet]
|
|
public IEnumerable<object> GetByMyCompany()
|
|
{
|
|
var currentId = User.Identity.GetUserId();
|
|
var ret= userService.GetByMyCompany(currentId);
|
|
return ret;
|
|
//var currentCompanyId = db.Users.Where(x => x.Id == currentId).Select(x => x.CompanyID).Single();
|
|
|
|
//return db.Users
|
|
// .Where(x => !x.IsSystemAdmin && !x.IsCompanyAdmin && x.CompanyID == currentCompanyId)
|
|
// .ToList();
|
|
}
|
|
|
|
[Route("api/User/isCompanyAdmin")]
|
|
[HttpGet]
|
|
public bool isCompanyAdmin()
|
|
{
|
|
var currentId = User.Identity.GetUserId();
|
|
var ret = userService.isCompanyAdmin(currentId);
|
|
return ret;
|
|
}
|
|
|
|
[Route("api/User/GetByUid/{uid}")]
|
|
[HttpGet]
|
|
public object GetByUid(string uid)
|
|
{
|
|
var currentId = User.Identity.GetUserId();
|
|
return userService.GetByUid(uid, currentId);
|
|
|
|
//var currentCompanyId = db.Users.Where(x => x.Id == currentId).Select(x => x.CompanyID).Single();
|
|
|
|
//return db.Users
|
|
// .Where(x => !x.IsSystemAdmin && !x.IsCompanyAdmin && x.CompanyID == currentCompanyId)
|
|
// .Where(x => x.Id == uid)
|
|
// .FirstOrDefault();
|
|
}
|
|
/// <summary>
|
|
/// 由angularJS呼叫過來刪除使用者之處
|
|
/// </summary>
|
|
/// <param name="toBeSave"></param>
|
|
/// <returns></returns>
|
|
[Route("api/User/Delete/{id?}")]
|
|
[HttpPost]
|
|
public int Delete(User toBeSave)
|
|
{
|
|
// 研究使用者如何建立
|
|
var currentId = User.Identity.GetUserId();
|
|
return userService.Delete(toBeSave, currentId);
|
|
}
|
|
/// <summary>
|
|
/// 由angularJS呼叫過來新建使用者之處
|
|
/// </summary>
|
|
/// <param name="toBeSave"></param>
|
|
/// <returns></returns>
|
|
[Route("api/User/Save/{id?}")]
|
|
[HttpPost]
|
|
public int Save(User toBeSave)
|
|
{
|
|
// 研究使用者如何建立
|
|
var currentId = User.Identity.GetUserId();
|
|
if (!_adminCheckService.adminCheckPass())
|
|
{
|
|
AdminAccess aa = _adminCheckService.getAdminAccess();
|
|
int lcaUserQuantity =
|
|
_adminCheckService.getLcaUserQuantity();
|
|
if (lcaUserQuantity >= aa.lcaUserLimit
|
|
&& aa.lcaUserLimit != -1)
|
|
throw new Exception(
|
|
$"盤查員帳號數({lcaUserQuantity})已達或超過全站限制" +
|
|
$"({aa.lcaUserLimit}),請系統管理員更新金鑰限制重新匯入");
|
|
var userid = User.Identity.GetUserId();
|
|
companyAdminAccessVM uaaVM = _companyAdminCheckService
|
|
.getCompanyAdminAccessVM(
|
|
_companyAdminCheckService.userId2companyID(userid));
|
|
int lcaUserQuantityByUserId = _companyAdminCheckService.getLcaUserQuantityByUserId(userid);
|
|
if (lcaUserQuantityByUserId >= uaaVM.lcaUserLimit
|
|
&& uaaVM.lcaUserLimit!=-1)
|
|
throw new Exception(
|
|
$"盤查員帳號數({lcaUserQuantityByUserId})已達或超過公司限制" +
|
|
$"({uaaVM.lcaUserLimit}),請公司管理員更新限制重新嘗試");
|
|
}
|
|
toBeSave.CreatedTime= DateTime.Now;
|
|
int ret = userService.Save(toBeSave, currentId);
|
|
return ret;
|
|
//var currentCompanyId = db.Users.Where(x => x.Id == currentId).Select(x => x.CompanyID).Single();
|
|
//toBeSave.CompanyID = currentCompanyId;
|
|
//toBeSave.IsCompanyAdmin = false;
|
|
//toBeSave.IsSystemAdmin = false;
|
|
|
|
//var user = manager.FindById(toBeSave.Id);
|
|
//if (user == null)
|
|
//{
|
|
// var result = manager.Create(toBeSave, toBeSave.DefaultPassword);
|
|
// if (!result.Succeeded)
|
|
// return -1;
|
|
// manager.AddToRole(toBeSave.Id, ProgramConstants.normalcompany);
|
|
//}
|
|
//else
|
|
//{
|
|
// user.Name = toBeSave.Name;
|
|
// user.Job = toBeSave.Job;
|
|
// user.Email = toBeSave.Email;
|
|
// user.PhoneNumber = toBeSave.PhoneNumber;
|
|
// user.MobileNumber = toBeSave.MobileNumber;
|
|
// db.SaveChanges();
|
|
//}
|
|
|
|
//return 0;
|
|
}
|
|
|
|
}
|
|
} |