Files

287 lines
10 KiB
C#
Raw Permalink Normal View History

using System;
using System.Collections.Generic;
using System.Linq;
using System.ServiceModel;
using BeWo.Data;
using BeWo.Service.Plugins;
using BS.Shared.DataContracts.Compact;
using BeWo.Data.Access;
using BeWo.Data.Entities;
using BeWo.Service.Core;
using BeWo.Service.DCEntityMapper;
using BeWo.Service.ServiceContracts;
using BS.Shared;
using BS.Shared.Core;
using BS.Shared.DataContracts;
using BS.SharedLauncher;
using BeWo.Service.Invoicing;
using System.IO;
using System.Configuration;
using System.Diagnostics;
using BeWo.Service.Configuration;
using BS.SharedLauncher.DataContract;
using BS.SharedLauncher.Information;
using Castle.Core;
namespace BeWo.Service.ServiceImplementations
{
[ServiceBehavior(InstanceContextMode = InstanceContextMode.PerCall, ConcurrencyMode = ConcurrencyMode.Single)]
public class LauncherServiceImp : ILauncherService
{
private const string sonderzeichen = "\\\"\'µ@€!§$%&/()=?+#*',.-;:_><|{[]}´`^°~²³";
private const string zahlen = "0123456789";
private const string grossBuchstaben = "ABCDEFGHIJKLMNOPQRSTUVWXYZÄÖÜ";
private const string kleinBuchstaben = "abcdefghijklmnopqrstuvwxyzäöüß";
public PasswordValidationResult CheckPassword(long userOid, string pOldPassword, string pNewPassword)
{
try
{
var user = DAOFactory.GenericDAO.LoadByID<ApplicationUser>(userOid);
if (pNewPassword.Equals(pOldPassword))
{
return PasswordValidationResult.AreTheSame;
}
if (!DAOFactory.UserDAO.CheckPassword(user, pOldPassword))
{
return PasswordValidationResult.NotCorrect;
}
var settings = AppSettings.CreateSettings();
if (settings.IsPasswordSecurityActiv)
{
if (pNewPassword.Length < 8)
{
return PasswordValidationResult.NotLongEnough;
}
if (pNewPassword.Equals("bewo"))
{
return PasswordValidationResult.ImFunnyAndUsingBewoAsPasswordSmile;
}
if (!CheckPasswordStrongEnough(pNewPassword))
{
return PasswordValidationResult.NotSpecialEnough;
}
}
return PasswordValidationResult.Succesful;
}
catch (Exception e)
{
throw Core.Utils.CreateBeWoFaultException(e);
}
}
public PasswordValidationResult CheckPassword2(string userName, string pOldPassword, string pNewPassword)
{
try
{
var allAppUser = DAOFactory.GenericDAO.GetAllActive<ApplicationUser>();
var id = allAppUser.ToList().Find(x => x.LoginName == userName).Oid.Value;
return CheckPassword(id, pOldPassword, pNewPassword);
}
catch (Exception e)
{
throw Core.Utils.CreateBeWoFaultException(e);
}
}
private bool CheckPasswordStrongEnough(string password)
{
return true;
//int richtlinienCount = 0;
//if (pNewPassword.Any(x => zahlen.Contains(x)))
//{
// richtlinienCount++;
//}
//if (pNewPassword.Any(x => grossBuchstaben.Contains(x)))
//{
// richtlinienCount++;
//}
//if (pNewPassword.Any(x => kleinBuchstaben.Contains(x)))
//{
// richtlinienCount++;
//}
//if (pNewPassword.Any(x => sonderzeichen.Contains(x)))
//{
// richtlinienCount++;
//}
////if (!(pNewPassword.Any(x => sonderzeichen.Contains(x)) && pNewPassword.Any(x => zahlen.Contains(x)) && pNewPassword.Any(x => grossBuchstaben.Contains(x)) && pNewPassword.Any(x => kleinBuchstaben.Contains(x))))
//if (richtlinienCount < 3)
//{
// return PasswordValidationResult.NotSpecialEnough;
//}
}
public long ChangePassword(string pUserName, string pOldPassword, string pNewPassword)
{
try
{
var user = DAOFactory.UserDAO.FindUserByLoginName(ConnectionInformation.Username);
var version = (new UserServiceImp()).ChangePassword(pUserName, user.Version.Value, pOldPassword, pNewPassword);
ResetPasswordInfo(pUserName, pOldPassword);
return 0;
}
catch (Exception e)
{
throw Core.Utils.CreateBeWoFaultException(e);
}
}
public void ResetPasswordInfo(string pUserName, string pPassword)
{
try
{
var user = DAOFactory.UserDAO.FindUserByLoginName(ConnectionInformation.Username);
user.ResetPasswordInfos.ForEach(f => f.HasToChangePW = false);
DAOFactory.GenericDAO.Update(user);
}
catch (Exception e)
{
throw Core.Utils.CreateBeWoFaultException(e);
}
}
public BeWoProgramPackageDC Update(BeWoProgramPackageDC current)
{
try
{
DirectoryInfo dirUpdateRoot = new DirectoryInfo(ConfigurationManager.AppSettings.Get("UpdateRootPath"));
var newest = GetNewestVersionSet(dirUpdateRoot, MultitenancyOperationContextExt.Current.Tenant);
var res = CreateUpdatePackage(current, newest);
res.toLoad = "BeWoPlaner.dll";
return res;
}
catch (Exception e)
{
throw Core.Utils.CreateBeWoFaultException(e);
}
}
private BeWoProgramPackageDC GetNewestVersionSet(DirectoryInfo dirUpdateRoot, string tenant)
{
var res = new BeWoProgramPackageDC();
var dirTenant = dirUpdateRoot.GetDirectories().FirstOrDefault(dict => dict.Name == tenant);
var filesRoot = dirUpdateRoot.GetFiles().Select(file => new BeWoProgramFileDC(file));
if (dirUpdateRoot is null)
throw new Exception("Couldn't find update root path for launcher updates");
res.Files.AddRange(filesRoot);
if (dirTenant is null)
return res;
foreach (var newFile in dirTenant.GetFiles().Select(file => new BeWoProgramFileDC(file)))
{
var search = res.Files.FirstOrDefault(x => x.FileName == newFile.FileName);
if (search is null)
{
res.Files.Add(newFile);
}
else
{
res.Files.Remove(search);
res.Files.Add(newFile);
}
}
return res;
}
private BeWoProgramPackageDC CreateUpdatePackage(BeWoProgramPackageDC oldSet, BeWoProgramPackageDC newSet)
{
var res = new BeWoProgramPackageDC();
if (!(oldSet is null))
res.Files.AddRange(oldSet.Files);
foreach (var newFile in newSet.Files)
{
var oldFile = res.Files.FirstOrDefault(x => x.FileName == newFile.FileName);
if (oldFile is null)
{
newFile.FileData = File.ReadAllBytes(newFile.FullPath);
newFile.ToDo = FileToDo.Add;
res.Files.Add(newFile);
}
else
{
if (oldFile.FileVersion is null || newFile.FileVersion is null)
{
oldFile.FileVersion = newFile.FileVersion;
oldFile.FileData = File.ReadAllBytes(newFile.FullPath);
oldFile.ToDo = FileToDo.Update;
}
else
{
if (oldFile.FileVersion == newFile.FileVersion)
{
if (oldFile.Checksum != newFile.Checksum)
{
oldFile.FileData = File.ReadAllBytes(newFile.FullPath);
oldFile.ToDo = FileToDo.Update;
}
else
{
oldFile.ToDo = FileToDo.Nuthing;
}
}
else
{
oldFile.FileVersion = newFile.FileVersion;
oldFile.FileData = File.ReadAllBytes(newFile.FullPath);
oldFile.ToDo = FileToDo.Update;
}
}
}
}
foreach (var oldFile in oldSet.Files)
{
var newFile = newSet.Files.FirstOrDefault(x => x.FileName == oldFile.FileName);
if (newFile is default(BeWoProgramFileDC))
{
oldFile.ToDo = FileToDo.Remove;
}
}
return res;
}
public UserValidationResult IsUserValid(string pUserName, string pPassword, string pPIN, string clientId, bool checkTenant)
=> (new UserServiceImp()).IsUserValid(pUserName, pPassword, pPIN, clientId, checkTenant);
public void ResetTenant(string tenant)
=> (new OperationsServiceImp()).ResetTenant(tenant);
public void ResetAllTenant()
=> (new OperationsServiceImp()).ResetAllTenants();
public bool GetEmailForUserName(string pUserName, Uri serverName, out string email)
=> (new UserServiceImp()).GetEmailForUserName(pUserName, serverName, out email);
}
}