using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;

public partial class MasterPages_Main : System.Web.UI.MasterPage
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (Session["uid"] == null)
            Response.Redirect("../Security/LoginPage.aspx");

        string[] temp;
        string response = "", message = "";
        temp = Request.Url.ToString().Split('/');

        string path = temp[temp.Length - 2] + "/" + temp[temp.Length - 1];
        if (path.Contains("?"))
        {
            path = path.Substring(0, path.IndexOf('?'));
        }
        string userID = Session["uid"].ToString();

        DataSet ds = SecurityAgent.authenticateUserPage(userID, path, ref response, ref message);

        if (!response.Equals("0"))
        {
            Response.Redirect("../Business/default.aspx?err=1");
        }

        applySecurity(ds);

        if (!IsPostBack)
        {
            Label2.Text = DateTime.Now.Date.ToShortDateString();
            Label3.Text = DateTime.Now.TimeOfDay.ToString().Split('.')[0];
            Label1.Text = Session["username"].ToString();
        }
    }

    private void applySecurity(DataSet ds)
    {
        string fn = "";
        string[] array;
        if (ds.Tables[0].Rows.Count == 0)
            return;
        for (int i = 0; i < ds.Tables[0].Rows.Count; i++)
        {
            fn = ds.Tables[0].Rows[i][3].ToString();
            if (fn[0].ToString().ToUpper().Equals("G"))
            {
                array = fn.Split('.');
                GridView gv = (GridView)ContentPlaceHolder1.FindControl(array[0]);
                gv.Columns[int.Parse(array[1])].Visible = false;
            }
            else
            {
                ContentPlaceHolder1.FindControl(fn).Visible = false;
            }
        }
    }
}
