326 lines
13 KiB
C#
326 lines
13 KiB
C#
|
using System;
|
|||
|
using System.Collections.Generic;
|
|||
|
using System.Data;
|
|||
|
using System.Data.Entity;
|
|||
|
using System.Linq;
|
|||
|
using System.Net;
|
|||
|
using System.Web;
|
|||
|
using System.Web.Mvc;
|
|||
|
using Microsoft.AspNet.Identity;
|
|||
|
using Microsoft.AspNet.Identity.EntityFramework;
|
|||
|
using Weee.DAL;
|
|||
|
using Weee.Models;
|
|||
|
using Weee.Supports;
|
|||
|
using PagedList;
|
|||
|
using Weee.ViewModels;
|
|||
|
using Qcarbon.Database.Lca;
|
|||
|
using CScommon;
|
|||
|
using Resources;
|
|||
|
using System.Threading;
|
|||
|
using System.Web.Routing;
|
|||
|
using Qcarbon.Interfaces.adminCheck;
|
|||
|
using NLog;
|
|||
|
using Qcarbon.Database.adminCheck;
|
|||
|
using Qcarbon.ViewModels.admin;
|
|||
|
|
|||
|
namespace Weee.Areas.Admin.Controllers
|
|||
|
{
|
|||
|
public class CompanyController : AdminControllerBase
|
|||
|
{
|
|||
|
private readonly EmailService.Service service = DependencyResolver.Current.GetService<EmailService.Service>();//CFT-36
|
|||
|
private LCAcommonService _LCAcommonService;
|
|||
|
protected IadminCheckService _adminCheckService;
|
|||
|
protected IcompanyAdminCheckService _companyAdminCheckService;
|
|||
|
protected Logger log;
|
|||
|
|
|||
|
public CompanyController(WeeeDataContext db)
|
|||
|
: base(db)
|
|||
|
{
|
|||
|
_LCAcommonService = new LCAcommonService(db);
|
|||
|
}
|
|||
|
protected override void Initialize(RequestContext requestContext)
|
|||
|
{
|
|||
|
base.Initialize(requestContext);
|
|||
|
var adminUserId = User.Identity.GetUserId();
|
|||
|
_adminCheckService = new adminCheckService(db, adminUserId);
|
|||
|
_companyAdminCheckService = new companyAdminCheckService(db, adminUserId);
|
|||
|
log = NLog.LogManager.GetCurrentClassLogger();
|
|||
|
}
|
|||
|
|
|||
|
public ActionResult Index(int page = 1, string searchString = "")
|
|||
|
{
|
|||
|
var qry = db.Companies.OrderByDescending(c => c.RegisterDate).ToList();//CFT-44
|
|||
|
List<CompanyViewModel> companies = new List<CompanyViewModel>();
|
|||
|
foreach(var rec in qry)
|
|||
|
{
|
|||
|
CompanyViewModel newRec = new CompanyViewModel();
|
|||
|
if (rec.ID == 18)
|
|||
|
Thread.Sleep(0);
|
|||
|
newRec.UserName = _LCAcommonService.companyUserName(rec.ID);
|
|||
|
newRec.CompanyType = rec.GetType();
|
|||
|
newRec.ID = rec.ID;
|
|||
|
newRec.UID = rec.UID;
|
|||
|
newRec.LogoUrl = rec.LogoUrl;
|
|||
|
newRec.Name = rec.Name;
|
|||
|
newRec.EnglishName = rec.EnglishName;
|
|||
|
newRec.Address = rec.Address;
|
|||
|
newRec.VATNumber = rec.VATNumber;
|
|||
|
newRec.CEOName = rec.CEOName;
|
|||
|
newRec.IndustryDescription = rec.IndustryDescription;
|
|||
|
newRec.Description = rec.Description;
|
|||
|
newRec.Capital = rec.Capital;
|
|||
|
newRec.WebSiteUrl = rec.WebSiteUrl;
|
|||
|
newRec.RegisterDate = rec.RegisterDate;
|
|||
|
newRec.LastStatusUpdateDate = rec.LastStatusUpdateDate;
|
|||
|
newRec.Status = rec.Status;
|
|||
|
companies.Add(newRec);
|
|||
|
}
|
|||
|
var dataFilter = new DataFilter();
|
|||
|
var filteredComapnies = dataFilter.FilterCompany(companies, searchString);
|
|||
|
var onePageOfCompanies = filteredComapnies.ToPagedList(page, 10);
|
|||
|
|
|||
|
ViewBag.SearchString = searchString;
|
|||
|
ViewBag.msg = TempData["msg"] + "";
|
|||
|
ViewBag.msg += TempData["adminCheckError"];
|
|||
|
return View(onePageOfCompanies);
|
|||
|
}
|
|||
|
|
|||
|
public ActionResult EditCompany(int? id)
|
|||
|
{
|
|||
|
if (id == null)
|
|||
|
{
|
|||
|
return new HttpStatusCodeResult(HttpStatusCode.BadRequest);
|
|||
|
}
|
|||
|
var company = db.NormalCompanies.Include(x=>x.Users).Where(x=>x.ID==id).SingleOrDefault();
|
|||
|
if (company == null)
|
|||
|
{
|
|||
|
var company2 = db.CertificationCompanies.Include(x => x.Users).Where(x => x.ID == id).SingleOrDefault();
|
|||
|
if (company2!=null)
|
|||
|
return RedirectToAction("EditCertification", new { id = id });
|
|||
|
return HttpNotFound();
|
|||
|
}
|
|||
|
return View(company);
|
|||
|
}
|
|||
|
|
|||
|
[HttpPost]
|
|||
|
[ValidateAntiForgeryToken]
|
|||
|
public ActionResult EditCompany(NormalCompany company)
|
|||
|
{
|
|||
|
if (ModelState.IsValid)
|
|||
|
{
|
|||
|
db.Entry(company).State = EntityState.Modified;
|
|||
|
db.Entry(company).Property(x => x.UID).IsModified = false;
|
|||
|
db.SaveChanges();
|
|||
|
return RedirectToAction("Index");
|
|||
|
}
|
|||
|
return View(company);
|
|||
|
}
|
|||
|
|
|||
|
// GET: /admin/Company/Edit/5
|
|||
|
public ActionResult EditCertification(int? id)
|
|||
|
{
|
|||
|
if (id == null)
|
|||
|
{
|
|||
|
return new HttpStatusCodeResult(HttpStatusCode.BadRequest);
|
|||
|
}
|
|||
|
var company = db.CertificationCompanies.Include(x => x.Users).Where(x => x.ID == id).SingleOrDefault();
|
|||
|
if (company == null)
|
|||
|
{
|
|||
|
return HttpNotFound();
|
|||
|
}
|
|||
|
return View(company);
|
|||
|
}
|
|||
|
|
|||
|
// POST: /admin/Company/Edit/5
|
|||
|
// To protect from overposting attacks, please enable the specific properties you want to bind to, for
|
|||
|
// more details see http://go.microsoft.com/fwlink/?LinkId=317598.
|
|||
|
[HttpPost]
|
|||
|
[ValidateAntiForgeryToken]
|
|||
|
public ActionResult EditCertification(CertificationCompany company)
|
|||
|
{
|
|||
|
// 查證單位在建立時不存在欄位'NumberOfEmployees',故移除該欄位驗證。
|
|||
|
ModelState.Remove("NumberOfEmployees");
|
|||
|
if (ModelState.IsValid)
|
|||
|
{
|
|||
|
if (checkCertificationCompanyNameExist(company.Name,company.ID))
|
|||
|
{
|
|||
|
ModelState.AddModelError(string.Empty, "公司名稱\"" + company.Name + "\"已存在");
|
|||
|
return View(company);
|
|||
|
}
|
|||
|
var entry = db.Entry(company);
|
|||
|
entry.State = EntityState.Modified;
|
|||
|
entry.Property(x => x.UID).IsModified = false;
|
|||
|
db.SaveChanges();
|
|||
|
return RedirectToAction("Index");
|
|||
|
}
|
|||
|
return View(company);
|
|||
|
}
|
|||
|
|
|||
|
// GET: /admin/Company/Delete/5
|
|||
|
public ActionResult Delete(int? id)
|
|||
|
{
|
|||
|
if (id == null)
|
|||
|
{
|
|||
|
return new HttpStatusCodeResult(HttpStatusCode.BadRequest);
|
|||
|
}
|
|||
|
Company company = db.Companies.Find(id);
|
|||
|
if (company == null)
|
|||
|
{
|
|||
|
return HttpNotFound();
|
|||
|
}
|
|||
|
return View(company);
|
|||
|
}
|
|||
|
|
|||
|
// POST: /admin/Company/Delete/5
|
|||
|
[HttpPost, ActionName("Delete")]
|
|||
|
[ValidateAntiForgeryToken]
|
|||
|
public ActionResult DeleteConfirmed(int id)
|
|||
|
{
|
|||
|
Company company = db.Companies.Find(id);
|
|||
|
db.Companies.Remove(company);
|
|||
|
db.SaveChanges();
|
|||
|
return RedirectToAction("Index");
|
|||
|
}
|
|||
|
|
|||
|
// actives a company
|
|||
|
[Weee.Filter.MvcLog]
|
|||
|
public ActionResult Active(int id)
|
|||
|
{
|
|||
|
var company = db.Companies.Find(id);
|
|||
|
var RoletoBeAdd = company.CompanyType == typeof(NormalCompany) ? ProgramConstants.normalcompany : ProgramConstants.certification;
|
|||
|
if (!_adminCheckService.adminCheckPass())
|
|||
|
{
|
|||
|
if (RoletoBeAdd== ProgramConstants.normalcompany)
|
|||
|
{
|
|||
|
AdminAccess aa = _adminCheckService.getAdminAccess();
|
|||
|
int companyAdminQuantity =
|
|||
|
_adminCheckService.getCompanyAdminQuantity();
|
|||
|
if (companyAdminQuantity>=aa.companyAdminLimit
|
|||
|
&& aa.companyAdminLimit!=-1)
|
|||
|
{
|
|||
|
TempData["adminCheckError"] =
|
|||
|
$"公司帳號數({companyAdminQuantity})已達或超過限制" +
|
|||
|
$"({aa.companyAdminLimit}),請系統管理員更新金鑰限制重新匯入";
|
|||
|
return RedirectToAction("index");
|
|||
|
}
|
|||
|
}
|
|||
|
else if (RoletoBeAdd == ProgramConstants.certification)
|
|||
|
{
|
|||
|
AdminAccess aa = _adminCheckService.getAdminAccess();
|
|||
|
int certifyCompanyQuantity =
|
|||
|
_adminCheckService.getCertifyCompanyQuantity();
|
|||
|
if (certifyCompanyQuantity >= aa.certifyCompanyLimit
|
|||
|
&& aa.certifyCompanyLimit != -1)
|
|||
|
{
|
|||
|
TempData["adminCheckError"] =
|
|||
|
$"查證帳號數({certifyCompanyQuantity})已達或超過限制" +
|
|||
|
$"({aa.certifyCompanyLimit}),請系統管理員更新金鑰限制重新匯入";
|
|||
|
return RedirectToAction("index");
|
|||
|
}
|
|||
|
}
|
|||
|
}
|
|||
|
if (company.Status==CompanyStatus.New || company.firstActivatedDate == null)
|
|||
|
company.firstActivatedDate = DateTime.Now;
|
|||
|
company.Status = CompanyStatus.Active;
|
|||
|
company.LastStatusUpdateDate = DateTime.Now;
|
|||
|
var UserManager = new UserManager<User>(new UserStore<User>(db));
|
|||
|
company.Users
|
|||
|
.ToList()
|
|||
|
.ForEach(x => UserManager.AddToRole(x.Id, RoletoBeAdd));
|
|||
|
db.SaveChanges();
|
|||
|
|
|||
|
//CFT-36
|
|||
|
//sent an email to the user
|
|||
|
var userInfo = db.Users.FirstOrDefault(u => u.CompanyID == id && u.IsCompanyAdmin == true);
|
|||
|
if(userInfo != null && userInfo.Email != null)
|
|||
|
{
|
|||
|
service.SendRequestMessaageToAdminOrUser(userInfo.Email
|
|||
|
, userInfo.UserName, "user", "activate", ViewBag.baseUrl);//CFT-42
|
|||
|
}
|
|||
|
|
|||
|
return RedirectToAction("index");
|
|||
|
}
|
|||
|
[Weee.Filter.MvcLog]
|
|||
|
//[HttpGet]
|
|||
|
//[Route("admin/Company/CompanyAdminAccess/{id}")]
|
|||
|
public ActionResult CompanyAdminAccess(int id)
|
|||
|
{
|
|||
|
companyAdminAccessVM viewModel =
|
|||
|
_companyAdminCheckService.getCompanyAdminAccessVM(id);
|
|||
|
return View(viewModel);
|
|||
|
}
|
|||
|
[Weee.Filter.MvcLog]
|
|||
|
[HttpPost]
|
|||
|
public ActionResult CompanyAdminAccess(companyAdminAccessVM viewModel)
|
|||
|
{
|
|||
|
if (!ModelState.IsValid)
|
|||
|
return View(viewModel);
|
|||
|
ViewBag.errMsg = _companyAdminCheckService.setCompanyAdminAccessVM(viewModel);
|
|||
|
if (string.IsNullOrWhiteSpace(ViewBag.errMsg))
|
|||
|
{
|
|||
|
ViewBag.OKmsg = "修改成功";
|
|||
|
viewModel = _companyAdminCheckService.getCompanyAdminAccessVM(viewModel.companyID);
|
|||
|
}
|
|||
|
return View(viewModel);
|
|||
|
}
|
|||
|
//de-actives a company
|
|||
|
[Weee.Filter.MvcLog]
|
|||
|
public ActionResult DeActive(int id)
|
|||
|
{
|
|||
|
var company = db.Companies.Find(id);
|
|||
|
company.Status = CompanyStatus.Inactive;
|
|||
|
var UserManager = new UserManager<User>(new UserStore<User>(db));
|
|||
|
var RoletoBeRemove = company.CompanyType == typeof(NormalCompany) ? ProgramConstants.normalcompany : ProgramConstants.certification;
|
|||
|
company.Users
|
|||
|
.ToList()
|
|||
|
.ForEach(x => UserManager.RemoveFromRole(x.Id, RoletoBeRemove));
|
|||
|
db.SaveChanges();
|
|||
|
//CFT-36
|
|||
|
//sent an email to the user
|
|||
|
var userInfo = db.Users.FirstOrDefault(u => u.CompanyID == id && u.IsCompanyAdmin == true);
|
|||
|
if (userInfo != null && userInfo.Email != null)
|
|||
|
{
|
|||
|
service.SendRequestMessaageToAdminOrUser(userInfo.Email
|
|||
|
, userInfo.UserName, "user", "deactivate", ViewBag.baseUrl);//CFT-42
|
|||
|
}
|
|||
|
|
|||
|
return RedirectToAction("index");
|
|||
|
}
|
|||
|
|
|||
|
[Weee.Filter.MvcLog]
|
|||
|
public ActionResult ResetPassword(int id)
|
|||
|
{
|
|||
|
var manager = DependencyResolver.Current.GetService<UserManager<Models.User>>();
|
|||
|
User user = _LCAcommonService.companyUser(id, false);
|
|||
|
if (!ModelState.IsValid || user == null)
|
|||
|
{
|
|||
|
TempData["msg"] = Resource.SaveFail;
|
|||
|
return RedirectToAction("index");
|
|||
|
}
|
|||
|
var hashedPassword = manager.PasswordHasher.HashPassword(
|
|||
|
CScommon.ProgramConstants.DefaultPassword);
|
|||
|
user.PasswordHash = hashedPassword;
|
|||
|
db.SaveChanges();
|
|||
|
TempData["msg"] = user.UserName +" "+ Resource.PasswordResetted;// Resource.SaveSuccess;
|
|||
|
return RedirectToAction("index");
|
|||
|
}
|
|||
|
protected override void Dispose(bool disposing)
|
|||
|
{
|
|||
|
if (disposing)
|
|||
|
{
|
|||
|
db.Dispose();
|
|||
|
}
|
|||
|
base.Dispose(disposing);
|
|||
|
}
|
|||
|
|
|||
|
private bool checkCertificationCompanyNameExist(string name, int id)
|
|||
|
{
|
|||
|
var exist = (from a in db.Companies
|
|||
|
join b in db.CertificationCompanies on a.ID equals b.ID
|
|||
|
where a.Name == name && a.ID != id
|
|||
|
select a).Any();
|
|||
|
return exist;
|
|||
|
}
|
|||
|
}
|
|||
|
}
|