public static dynamic GetAuthroizedUser()
        {
            dynamic CurrentUser = null;
            try
            {
                HttpSessionState session = HttpContext.Current.Session;
                string model = session[ "Model" ].ToString();
                int id = Int32.Parse( session[ "UserId" ].ToString() );
                if ( model.Equals( "Agent" ) )
                {
                    AgentModel connector = new AgentModel();
                    CurrentUser = connector.SelectById( id );
                }
                else if ( model.Equals( "Client" ) )
                {
                    ClientModel connector = new ClientModel();
                    CurrentUser = connector.SelectById( id );
                }
            }
            catch
            {
                return null;
            }

            return CurrentUser;
        }
 /// <summary>
 /// Test if nickname is unique
 /// </summary>
 /// <param name="model">Model object for this connector</param>
 /// <returns>True if model is unique</returns>
 public bool IsUniqueName(ClientModel model)
 {
     KeyValuePair<string, string> arg = new KeyValuePair<string,string>("Name", model.Name);
     string sql = "SELECT COUNT(*) FROM " + TableName;
     int count = ExecuteCustomQuery(QueryType.Count, sql, arg);
     if (count == 0)
         return true;
     return false;
 }
        public ActionResult Delete( int id )
        {
            try
            {
                IUser currentUser = Authorization.GetAuthroizedUser();

                if ( currentUser.IsSuperuser() )
                {
                    ClientModel model = new ClientModel();
                    model.DeleteById( id );
                }
            }
            catch
            {
                return RedirectToAction( "Index" );
            }
            return RedirectToAction( "Index" );
        }
        public ActionResult Create(ClientModel client)
        {
            if (ModelState.IsValid)
            {
                string passportInfo = Request["PassportInfo"];
                // Validate passport info input field if Model fields are valid
                if (passportInfo.Equals("") || passportInfo.Replace(" ", "").Equals(""))
                {
                    ModelState.AddModelError("PassportInfo", "Требуется поле Passport Information.");
                    return View(client);
                }

                // TODO: Rewrite as complex SQL query
                // Insert passport info
                PassportModel passportModel = new PassportModel();
                passportModel.PassportInfo = passportInfo;
                int passportId = passportModel.Insert( passportModel );

                // Validate unique name of client
                if (!client.IsUniqueName(client))
                {
                    ModelState.AddModelError("Client", "Такой пользователь уже существует.");
                    return View("CreateClient", client);
                }
                int clientId = client.Insert(client);

                // Create connection between passport and client
                PassportToClientModel connectionModel = new PassportToClientModel();
                connectionModel.ClientId = clientId;
                connectionModel.PassportId = passportId;
                connectionModel.ChangingDate = DateTime.Now;
                connectionModel.Insert( connectionModel );

                return RedirectToRoute("/");

            }
            return View(client);
        }
        public ActionResult ProcessLogin(FormCollection formCollection)
        {
            // Recieve form variables through post data
            string name = formCollection["Name"];
            string model = formCollection["SelectUser"];

            int id = -1;
            // Check if it is client or agent request authorization
            // f**k DRY, no dynamic outside base classes
            if (model.Equals("Client"))
            {
                ClientModel clientConnector = new ClientModel();
                id = clientConnector.ExecuteCustomQuery(
                    BaseSqlDatabaseModel.QueryType.Id,
                    "SELECT ClientId FROM " + clientConnector.TableName,
                    new KeyValuePair<string, string>("Name", name)
                );
            }
            else if (model.Equals("Agent"))
            {
                AgentModel agentConnector = new AgentModel();
                id = agentConnector.ExecuteCustomQuery(
                    BaseSqlDatabaseModel.QueryType.Id,
                    "SELECT AgentId FROM " + agentConnector.TableName,
                    new KeyValuePair<string, string>("Name", name)
                );
            }
            if (id != -1)
            {
                Session["Model"] = model;
                Session["UserId"] = id;
                return Redirect("/");
            }

            return View("LogIn");
        }
 public ActionResult Edit( int id )
 {
     try
     {
         IUser currentUser = Authorization.GetAuthroizedUser();
         if ( currentUser == null )
             return Redirect( "/" );
         if ( currentUser.IsAgent() )
         {
             ClientModel model = new ClientModel();
             model = model.SelectById( id );
             return View( model );
         }
     }
     catch
     {
         return RedirectToAction( "Index" );
     }
     return RedirectToAction( "Index" );
 }
        public ActionResult Index()
        {
            IUser currentUser = Authorization.GetAuthroizedUser();
            if ( currentUser == null )
                return Redirect( "/" );

            try
            {
                if ( currentUser.IsAgent() )
                {
                    ClientModel model = new ClientModel();
                    List<ClientModel> tariffList = model.SelectAll().ConvertAll( x => ( ClientModel ) x );
                    ViewData[ "clientList" ] = tariffList;
                    return View();
                }
            }
            catch
            {
                ViewData[ "clientList" ] = new List<ClientModel>();
                return View();
            }
            return Redirect( "/" );
        }
 public ActionResult Edit( int id, ClientModel model )
 {
     try
     {
         IUser currentUser = Authorization.GetAuthroizedUser();
         if ( currentUser.IsSuperuser() && ModelState.IsValid )
         {
             model.Update( id, model );
             return RedirectToAction( "Index" );
         }
     }
     catch
     {
         return RedirectToAction( "Index" );
     }
     return View( model );
 }