demo20230512/Controllers/ProfileController.cs
2023-05-12 10:20:28 +08:00

303 lines
10 KiB
C#

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using Microsoft.AspNet.Identity;
using Weee.Filter;
using Weee.DAL;
using Weee.ViewModels;
using Weee.Models;
using Resources;
using CScommon;
using System.Text.RegularExpressions;
using Weee.Models.ExtensionMethods;
using Customize.Insynerger.Interfaces;
using Customize.Insynerger;
using Weee.Models.Customize.Insynerger;
using NPOI.SS.Formula.Functions;
using System.Configuration;
namespace Weee.Controllers
{
[Authorize(Roles = ProgramConstants.normalcompany)]
//[MvcMultilanguage]disable obsolete warning, not sure OK or not
public class ProfileController : QcarbonControllerBase
{
//private WeeeDataContext _db;
private readonly EmailService.Service service = DependencyResolver.Current.GetService<EmailService.Service>();//CFT-45
private readonly string baseUrl = "";
public ProfileController(WeeeDataContext db)
: base(db)
{
//_db = db;
baseUrl = ConfigurationManager.AppSettings["InsynergerAPIurl"];
}
[Route("Profile")]
public ActionResult Index()
{
var id = User.Identity.GetUserId();
IinsynergerAuthenticate isa = new insynergerAuthenticate(db, baseUrl);
ViewBag.SynergerOn = isa.IsSynergerOn();
if (ViewBag.SynergerOn)
{
var vm = (from a in db.Users
join x in db.SynergerUser on a.Id equals x.userId into lj
from b in lj.DefaultIfEmpty()
where a.Id == id
select new ProfileViewModel
{
Name = a.Name,
Email = a.Email,
Job = a.Job,
PhoneNumber = a.PhoneNumber,
MobileNumber = a.MobileNumber,
SynergerGroupId = b.groupId
}).SingleOrDefault();
if (vm == null) return RedirectToAction("index", "home", new { });
return View(vm);
}
else
{
var vm = db.Users
.Where(x => x.Id == id)
.Select(x => new ProfileViewModel()
{
Name = x.Name,
Email = x.Email,
Job = x.Job,
PhoneNumber = x.PhoneNumber,
MobileNumber = x.MobileNumber
})
.SingleOrDefault();
if (vm == null) return RedirectToAction("index", "home", new { });
return View(vm);
}
}
[HttpPost]
[Route("Profile")]
public ActionResult Index(ProfileViewModel vm)
{
IinsynergerAuthenticate isa = new insynergerAuthenticate(db, baseUrl);
ViewBag.SynergerOn = isa.IsSynergerOn();
if (!ModelState.IsValid)
return View(vm);
var trans= db.Database.BeginTransaction();
try
{
var id = User.Identity.GetUserId();
var user = db.Users.Where(x => x.Id == id)
.SingleOrDefault();
user.Name = vm.Name;
user.Email = vm.Email;
user.Job = vm.Job;
user.PhoneNumber = vm.PhoneNumber;
user.MobileNumber = vm.MobileNumber;
if (ViewBag.SynergerOn)
{
var synu=db.SynergerUser
.Where(x => x.userId==id && x.isSelf==true)
.SingleOrDefault();
if (synu != null)
db.SynergerUser.Remove(synu);
if (vm.SynergerGroupId != null)
{
SynergerUser su = new SynergerUser();
su.userId = id;
su.isSelf = true;
su.groupId =(int)vm.SynergerGroupId;
db.SynergerUser.Add(su);
}
}
db.SaveChanges();
trans.Commit();
ViewBag.SuccessMessage = Resource.SaveSuccess;
}
catch(Exception ex)
{
trans.Rollback();
throw;
}
return View(vm);
}
[Route("Profile/changepassword")]
public ActionResult ChangePassword()
{
return View();
}
[HttpPost]
[Route("Profile/changepassword")]
[ValidateAntiForgeryToken]
public ActionResult ChangePassword(ViewModels.ProfileChangePasswordViewModel vm)
{
var manager = DependencyResolver.Current.GetService<UserManager<Models.User>>();
var userName = User.Identity.GetUserName();
var user = manager.Find(userName, vm.OriginalPassword ?? "");
if (!ModelState.IsValid || user == null)
{
ViewBag.FailMessage = Resource.SaveFail;
return View(new ProfileChangePasswordViewModel());
}
string ck = PassWordUtl.PasswordCheck(vm.NewPassword);
if (!string.IsNullOrWhiteSpace(ck))
{
ViewBag.FailMessage = ck;
return View(new ProfileChangePasswordViewModel());
}
var hashedPassword = manager.PasswordHasher.HashPassword(vm.NewPassword);
user.PasswordHash = hashedPassword;
db.SaveChanges();
ViewBag.SuccessMessage = Resource.SaveSuccess;
return View(new ProfileChangePasswordViewModel());
}
[Route("Profile/changepassword2")]
[AllowAnonymous]
public ActionResult ChangePassword2(string key)
{
try
{
if (key != null && key.Trim() != "")
{
string userName = service.Decrypt(key);
var userInfo = db.Users.FirstOrDefault(u => u.UserName == userName);
if (userInfo == null)
{
ViewData["Message"] = Resource.AccountNotExist;
}
else
{
ViewData["UserName"] = userName;
}
}
else
{
ViewBag.SuccessMessage = Resource.SaveSuccess;
ViewData["Message"] = Resource.AccountNotExist;
}
}
catch (Exception ex)
{
ex = CScommon.Exceptions.inner(ex);
ViewData["Message"] = ex.Message;
}
return View();
}
[HttpPost]
[Route("Profile/changepassword2")]
[AllowAnonymous]
[ValidateAntiForgeryToken]
public ActionResult ChangePassword2(ViewModels.ResetPasswordViewModel vm)
{
var manager = DependencyResolver.Current.GetService<UserManager<Models.User>>();
var user = db.Users.FirstOrDefault(u => u.UserName == vm.UserName);
if (!ModelState.IsValid || user == null)
{
ViewBag.FailMessage = Resource.SaveFail;
return View(new ResetPasswordViewModel());
}
var hashedPassword = manager.PasswordHasher.HashPassword(vm.NewPassword);
user.PasswordHash = hashedPassword;
db.SaveChanges();
ViewBag.SuccessMessage = Resource.SaveSuccess;
return View(new ResetPasswordViewModel());
}
[HttpGet]
[Route("Profile/Company")]
public ActionResult Company()
{
string userId = User.Identity.GetUserId();
var user = db.Users.Where(x => x.Id == userId).First();
var company = user.Company;
return View(company);
}
[HttpPost]
[Route("Profile/Company")]
[ValidateAntiForgeryToken]
public ActionResult Company(NormalCompany input)
{
string userId = User.Identity.GetUserId();
Company company = db.Users.Where(x => x.Id == userId).First().Company;
if (ModelState.IsValid)
{
company.Name = input.Name;
company.EnglishName = input.EnglishName;
company.Address = input.Address;
company.BusinessAddress = input.BusinessAddress;
company.WebSiteUrl = input.WebSiteUrl;
company.VATNumber = input.VATNumber;
company.NumberOfEmployees = input.NumberOfEmployees;
company.Industry = input.Industry;
company.CEOName = input.CEOName;
company.Capital = input.Capital;
company.Description = input.Description;
company.IndustryDescription = input.IndustryDescription;
db.SaveChanges();
ViewBag.SuccessInfo = Resource.UpdateSuccess;
}
return View(company);
}
[HttpGet]
[Route("Profile/Settings")]
public ActionResult Settings()
{
string userId = User.Identity.GetUserId();
UserAccountType accountType = db.UserAccountType.Where(x => x.UserId == userId).FirstOrDefault();
if(accountType.tCO2digits == null)
{
accountType.tCO2digits = 0;
}
return View(accountType);
}
[HttpPost]
[Route("Profile/Settings")]
[ValidateAntiForgeryToken]
public ActionResult Settings(UserAccountType input)
{
string userId = User.Identity.GetUserId();
UserAccountType accountType = db.UserAccountType.Where(x => x.UserId == userId).FirstOrDefault();
if (ModelState.IsValid)
{
accountType.tCO2digits = input.tCO2digits;
db.SaveChanges();
ViewBag.SuccessInfo = Resource.UpdateSuccess;
}
return View(accountType);
}
[HttpPost]
[Route("Profile/SetSession")]
public void SetSession(string sessionName,string sessionValue)
{
Session[sessionName] = sessionValue;
}
}
}