//------------------------------------------------------------------------------
// <copyright company="Telligent Systems">
//     Copyright (c) Telligent Systems Corporation.  All rights reserved.
// </copyright> 
//------------------------------------------------------------------------------

using System;
using System.Collections;
using System.Security;
using System.Web.Services;
using System.Web.Services.Protocols;

namespace CommunityServer.Components
{
	/// <summary>
	/// Summary description for CSWebServiceBase.
	/// </summary>
	public abstract class CSWebServiceBase : WebService
	{
		public CSWebServiceBase()
		{
		}

		public class ServiceCredentials : SoapHeader {
			public string Username;
			public string Password;
			public string SectionName; 
		}

		protected void LoginSetSection() {
			Login();

			AllocateSection();

		}

		protected void Login() {
			if(Globals.IsNullorEmpty(Credentials.Username) || Globals.IsNullorEmpty(Credentials.Password))
				throw new SecurityException("Invalid (or unsupplied) Username, or Password");
	
			User u = new User();
			u.Username = Credentials.Username;
			u.Password = Credentials.Password;
			LoginUserStatus status = Users.ValidUser(u);
			if(status == LoginUserStatus.Success) {
				System.Web.Security.FormsAuthentication.SetAuthCookie(u.Username,false);
				CurrentUser = Users.GetUser(-1,u.Username,false,false);
				CSContext.Current.User = CurrentUser;
			}
			else
				throw new SecurityException("Invalid User Credentials");
		}

		protected abstract void AllocateSection();

		protected User CurrentUser = null;
		public ServiceCredentials Credentials = new ServiceCredentials();

		[WebMethod(MessageName="Validate",Description="Validates a users credentials for a specific Section",EnableSession=false)]
		[SoapHeader("Credentials")]
		public virtual bool Validate()
		{
			LoginSetSection();

			return true;
		}

		[WebMethod(MessageName="Ping",Description="Enables pinging to see if the site is online",EnableSession=false,CacheDuration=3600)]
		public void Ping()
		{
		}
	}
}
