www.pudn.com > Tustena_CRM_OS_3.0.2_Stable.zip > UserData.cs


/// TUSTENA PUBLIC LICENSE v1.0 
///  
/// Portions Copyright (c) 2003-2005 Digita S.r.l. All Rights Reserved. 
/// 
/// Tustena CRM is a trademark of: 
/// Digita S.r.l. 
/// Viale Enrico Fermi 14/z 
/// 31011 Asolo (Italy) 
/// Tel. +39-0423-951251 
/// Mail. info@digita.it 
/// 
/// This file contains Original Code and/or Modifications of Original Code 
/// as defined in and that are subject to the Tustena Public Source License 
/// Version 1.0 (the 'License'). You may not use this file except in 
/// compliance with the License. Please obtain a copy of the License at 
/// http://www.tustena.com/TPL/ and read it before using this 
// file. 
/// 
/// The Original Code and all software distributed under the License are 
/// distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER 
/// EXPRESS OR IMPLIED, AND DIGITA S.R.L. HEREBY DISCLAIMS ALL SUCH WARRANTIES, 
/// INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY, 
/// FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT. 
/// Please see the License for the specific language governing rights and 
/// limitations under the License. 
/// 
/// YOU MAY NOT REMOVE OR ALTER THIS COPYRIGHT NOTICE! 
///  
 
using System; 
using System.Data; 
 
using System.Globalization; 
using System.Text; 
using System.Threading; 
using System.Web; 
using Digita.Tustena.Base.SideBar; 
using Digita.Tustena.Database; 
using Win32; 
 
namespace Digita.Tustena.Base 
{ 
	public class UserData 
	{ 
 
		public static UserConfig LoadPersonalData(string userName, string passWord, int customerId) 
		{ 
			string strSQL; 
			strSQL = "SELECT UserAccount, Password, GroupID, UID, ManagerId, convert(varchar(5),WorkStart_1,114) as StartWorkHour, convert(varchar(5),WorkEnd_2,114) as EndWorkHour, OfficeID, (Account.Name+' '+Account.Surname) as UserName, DiaryAccount, OfficeAccount, datepart(hh,WorkStart_1) as starthour, datepart(hh,WorkEnd_1) as endhour, TimeZones.MTimeZone, TimeZones.HTimeZone, TimeZones.DayLightSavingStart, TimeZones.DayLightSavingEnd, TimeZones.MDayLight, datepart(hh,WorkStart_2) as starthour2, datepart(hh,WorkEnd_2) as endhour2, WorkDays, PersistLogin, TimeZones.DTZ, TimeZones.DayLightSavingStart, TimeZones.DayLightSavingEnd, Paging, FullScreen, ViewBirthDate, Culture,  Account.active, Account.SessionTimeout, Account.InsertGroups, Account.FirstDayOfWeek, Account.OtherGroups, Tustena_Data.Email as CustomerEmail, Tustena_Data.debugmode from Account LEFT OUTER JOIN TimeZones ON Account.TimeZone = TimeZones.ShortName cross join Tustena_Data "; 
 
			if(customerId>-1) 
			{ 
				strSQL += " WHERE (UserAccount=@username and Password=@password and Tustena_Data.Active=1) "; 
			} 
			else 
			{ 
				strSQL += " WHERE (UserAccount=@username and Password=@password and Tustena_Data.Active=1) "; 
			} 
 
			DbSqlParameterCollection param = new DbSqlParameterCollection(); 
			param.Add(new DbSqlParameter("@username", userName)); 
			param.Add(new DbSqlParameter("@password", passWord)); 
 
 
			DataSet ds = DatabaseConnection.SecureCreateDataset(strSQL,param); 
			if (ds.Tables[0].Rows.Count>0) 
			{ 
				DataRow dr = ds.Tables[0].Rows[0]; 
				bool activeCompany = true; 
				bool inTest = false; 
				bool activeAccount = Convert.ToBoolean(dr["active"]); 
				DataRow acBool; 
				acBool = DatabaseConnection.CreateDataset("select * from Tustena_Data").Tables[0].Rows[0]; 
 
		activeCompany=true; 
 
				string cc = Thread.CurrentThread.CurrentCulture.DisplayName; 
				if (activeCompany && activeAccount) 
				{ 
					UserConfig UC = new UserConfig(); 
					UC.UserName = dr["UserAccount"].ToString(); 
					UC.UserGroupId = Convert.ToInt32(dr["GroupID"]); 
					UC.Logged = LoggedStatus.yes; 
					UC.UserId = Convert.ToInt32(dr["UID"]); 
					UC.MyBossId = Convert.ToInt32(dr["ManagerID"]); 
					UC.WorkStartHour = (string)dr["StartWorkHour"]; 
					UC.WorkEndHour = (string)dr["EndWorkHour"]; 
					UC.Office = (int)dr["OfficeID"]; 
					UC.UserRealName = (string)dr["UserName"]; 
					UC.UserAgenda = (string)dr["DiaryAccount"]; 
					UC.OfficeAgenda = (string)dr["OfficeAccount"]; 
					UC.StartHourAM = (int)dr["StartHour"]; 
					UC.EndHourAM = (int)dr["EndHour"]; 
					UC.StartHourPM = (int) dr["starthour2"]; 
					UC.EndHourPM = (int) dr["endhour2"]; 
					UC.WeekWorkDays = (Byte) dr["WorkDays"]; 
					UC.PagingSize = int.Parse(dr["Paging"].ToString()); 
					UC.FullScreen = (bool) dr["FullScreen"]; 
					UC.ViewBirthDate = (bool) dr["ViewBirthDate"]; 
					UC.ImpersonateUser = 0; 
					UC.ImpersonateOffice = 0; 
					UC.DebugMode = (bool) dr["debugmode"]; 
					UC.InsertGroups = (dr["InsertGroups"] == DBNull.Value) ? "" : (string) dr["InsertGroups"]; 
					UC.FirstDayOfWeek = (bool) dr["FirstDayOfWeek"]; 
 
					UC.MailingAddress = (string) dr["CustomerEmail"]; 
 
					if ((dr["DayLightSavingStart"].ToString() != null && dr["DayLightSavingStart"].ToString().Length != 0)) 
					{ 
						if (DateTime.Now >= (DateTime) dr["DayLightSavingStart"] && DateTime.Now <= (DateTime) dr["DayLightSavingEnd"]) 
						{ 
							UC.HTimeZone = (double) dr["HTimeZone"] + (double) dr["DTZ"]; 
						} 
						else 
						{ 
							UC.HTimeZone = (double) dr["HTimeZone"]; 
						} 
					} 
					else 
					{ 
						UC.HTimeZone = (double) dr["HTimeZone"]; 
					} 
 
					if (HttpContext.Current.Request.Cookies["CulturePref"] == null) 
						UC.Culture = dr["Culture"].ToString(); 
					else if (HttpContext.Current.Request.Cookies["CulturePref"].ToString().Length > 1) 
						UC.Culture = HttpContext.Current.Request.Cookies["CulturePref"].Value.ToString(); 
					else 
						UC.Culture = CultureInfo.CurrentUICulture.Name; 
 
					UC.CultureSpecific = UC.Culture.Substring(UC.Culture.Length - 2).ToLower(); 
 
					UC.PersistLogin = (bool) dr["PersistLogin"]; 
					HttpContext.Current.Session.Timeout = (int) dr["SessionTimeout"]; 
 
					StringBuilder othergroups = new StringBuilder(); 
					othergroups.AppendFormat("id={0} or dependency like '%|{0}|%' or ", dr[2].ToString()); 
					string[] OG = dr["OtherGroups"].ToString().Split('|'); 
					if (OG.Length > 0) 
					{ 
						foreach (string ogid in OG) 
							if (ogid.Length > 0) 
								othergroups.AppendFormat("id={0} or dependency like '%|{0}|%' or ", ogid); 
					} 
 
					othergroups.Remove(othergroups.Length - 3, 3); 
					string sqlString = "select id,dependency from Groups where " + othergroups.ToString(); 
 
					DataSet dsGroup = DatabaseConnection.CreateDataset(sqlString); 
					string dependency = "|"; 
					if (dsGroup.Tables[0].Rows.Count > 0) 
					{ 
						foreach (DataRow drdip in dsGroup.Tables[0].Rows) 
						{ 
							dependency += drdip["id"].ToString() + "|"; 
						} 
					} 
					if (dependency.Length > 1) 
					{ 
						UC.GroupDependency = dependency; 
					} 
					else 
					{ 
						UC.GroupDependency = "0"; 
					} 
 
					DataSet DSAdmGrID; 
					DSAdmGrID = DatabaseConnection.CreateDataset("select AdminGroupID from Tustena_Data"); 
					UC.AdminGroupId = (long) DSAdmGrID.Tables[0].Rows[0][0]; 
 
					UC.LTZ = TimeZones.GetTimeZone(Convert.ToInt32(DatabaseConnection.SqlScalar("select TimeZoneIndex from account where uid=" + UC.UserId))); 
					UC.myDTFI = new CultureInfo(UC.Culture).DateTimeFormat; 
 
					RemapModules(); 
 
					return UC; 
				} 
				else 
				{ 
					UserConfig UC=new UserConfig(); 
					if(inTest) 
						UC.Logged=LoggedStatus.testing; 
					else 
						UC.Logged=LoggedStatus.no; 
					return UC; 
				} 
			} 
			else 
			{ 
				UserConfig UC=new UserConfig(); 
				UC.Logged=LoggedStatus.no; 
				return UC; 
			} 
		} 
 
		private static void RemapModules() 
		{ 
			int selectedModulesCount = 3; 
			int i=0; 
			SideBarModule[] sbma = ((SideBarModule[])HttpContext.Current.Application["modules"]); 
			SideBarModule[] lsbma = new SideBarModule[selectedModulesCount]; 
			foreach(SideBarModule sbm in sbma) 
			{ 
				if(sbm.moduleId!="") 
					lsbma[i++]=sbm; 
			} 
			HttpContext.Current.Session["modules"] = lsbma; 
		} 
	} 
}