예제 #1
0
        public static void check_sold_item(AccessControl.AccessManager mgr, int idx, string type, int package_idx)
        {
            shop_itemTableAdapters.shop_itemTableAdapter ta = new shop_itemTableAdapters.shop_itemTableAdapter();
            ta.SqlConnection = (System.Data.SqlClient.SqlConnection)mgr.Connection;

            Shop.shop_sold_itemTableAdapters.shop_sold_itemTableAdapter si_ta = new Shop.shop_sold_itemTableAdapters.shop_sold_itemTableAdapter();
            si_ta.SqlConnection = (System.Data.SqlClient.SqlConnection)mgr.Connection;


            shop_item.shop_itemDataTable p = ta.GetRowByIdx(package_idx);
            if (0 == p.Rows.Count)
            {
                throw new System.Exception("Has no such package");
            }

            Shop.shop_sold_item.shop_sold_itemDataTable psi = si_ta.GetByItt(package_idx, type2code("group"), mgr.UserGuid);
            if (psi.Rows.Count == 0 && "group" == type)
            {
                throw new System.Exception("Package was not bought");
            }

            if (type != "group")
            {
                Shop.shop_sold_item.shop_sold_itemDataTable csi = si_ta.GetByItt(idx, type2code(type), mgr.UserGuid);
                // let's merge

                foreach (Shop.shop_sold_item.shop_sold_itemRow r in csi)
                {
                    if (r.sold_at == r.expires_at)
                    {
                        csi[0].sold_at = csi[0].expires_at;
                    }
                }

                if (p[0].limit != 0)
                {
                    if (psi.Rows.Count == 0)
                    {
                        throw new System.Exception("Unexpected condition");
                    }
                    bool has_nb = false;
                    if (csi.Rows.Count != 0)
                    {
                        foreach (Shop.shop_sold_item.shop_sold_itemRow i in csi)
                        {
                            if (i.parent == psi[0].idx)
                            {
                                has_nb = true;
                            }
                        }
                    }

                    if (!has_nb)
                    {
                        // item is selected from the limited package
                        if (psi[0].use_count == psi[0].sold_count)
                        {
                            throw new System.Exception("Limit reached");
                        }
                        purchase(mgr, idx, type, package_idx, 0);
                        psi[0].use_count++;
                        si_ta.Update(psi);
                        return;
                    }
                }
                if (csi.Rows.Count == 0)
                {
                    throw new System.Exception("Item was not bought");
                }

                if (csi[0].expires_at < DateTime.Now && csi[0].expires_at != csi[0].sold_at)
                {
                    mgr.revoke_access_to_function(String.Format("item_{0}_{1}", type, idx));
                    throw new System.Exception("Access time was expired");
                }
            }
        }
예제 #2
0
        protected static void purchase_(AccessControl.AccessManager mgr, IDbConnection conn, Guid guididx, int idx, string type, int package_idx, decimal payed)
        {
            string s = String.Format("item_{0}_{1}", type, idx);


            shop_itemTableAdapters.shop_itemTableAdapter sh_it_ta = new shop_itemTableAdapters.shop_itemTableAdapter();
            sh_it_ta.SqlConnection = (System.Data.SqlClient.SqlConnection)(conn);

            shop_sold_itemTableAdapters.shop_sold_itemTableAdapter sh_sit_ta = new shop_sold_itemTableAdapters.shop_sold_itemTableAdapter();
            sh_sit_ta.SqlConnection = (System.Data.SqlClient.SqlConnection)(conn);

            shop_item.shop_itemDataTable p = sh_it_ta.GetRowByIdx(package_idx);
            if (p.Rows.Count == 0)
            {
                throw new System.Exception("No such item to purchase");
            }

            shop_sold_item.shop_sold_itemDataTable sh_sit = sh_sit_ta.GetData();
            shop_sold_item.shop_sold_itemDataTable p_sit  = sh_sit_ta.GetByItt(package_idx, 1, guididx);
            int prnt_idx = -1;

            // if item bought from a package we must select a package idx to bind to
            if (p_sit.Rows.Count != 0)
            {
                prnt_idx = p_sit[0].idx;
            }

            string name = "";

            if ("group" == type)
            {
                name = "Access to package: " + get_package_name_id_((System.Data.SqlClient.SqlConnection)conn, null, idx);

                int pidx_ = -1;

                if (p[0]["parent_idx"].GetType() != typeof(System.DBNull))
                {
                    pidx_ = p[0].parent_idx;
                }

                sh_sit.Addshop_sold_itemRow(p[0].idx, DateTime.Now, DateTime.Now.AddMinutes(p[0].expires_after), 0, p[0].limit, guididx, (int)1, 0, p[0].name, pidx_, payed);
                payed = 0;

                int prn = sh_sit[sh_sit.Rows.Count - 1].idx;

                {
                    mgr.check_function_and_group(s, "Access to " + name);
                    mgr.grant_access_to_function(s);
                }

                // for unlimited package we must grant access to all it's contents
                if (p[0].limit == 0)
                {
                    System.Collections.ArrayList arr = get_item_cont_by_idx((System.Data.SqlClient.SqlConnection)(conn), null, idx);
                    foreach (System.Collections.Hashtable i in arr)
                    {
                        if (i["bound"] == null)
                        {
                            continue;
                        }
                        if (i["bound"].GetType() == typeof(System.DBNull))
                        {
                            continue;
                        }
                        if ((int)i["bound"] != idx)
                        {
                            continue;
                        }
                        if ((int)i["type"] == 2)
                        {
                            name = "Test: " + get_test_name_by_id_((System.Data.SqlClient.SqlConnection)conn, null, (int)i["idx"]);
                            string f = String.Format("item_{0}_{1}", "test", i["idx"]);
                            mgr.check_function_and_group(f, name);
                            mgr.grant_access_to_function(f);
                            //LogManager.GetLogger("Shop.purchase").WarnFormat("Access granted for '{0}' to {1}", mgr.UserLogin, name);
                            sh_sit.Addshop_sold_itemRow((int)i["idx"], DateTime.Now, DateTime.Now.AddMinutes(p[0].expires_after), 0, 0, guididx, (int)2, prn, i["name"].ToString(), p[0].idx, 0);
                        }
                        if ((int)i["type"] == 3)
                        {
                            name = "Download: " + get_download_name_by_id_((System.Data.SqlClient.SqlConnection)conn, null, (int)i["idx"]);
                            string f = String.Format("item_{0}_{1}", "download", i["idx"]);
                            mgr.check_function_and_group(f, name);
                            mgr.grant_access_to_function(f);
                            sh_sit.Addshop_sold_itemRow((int)i["idx"], DateTime.Now, DateTime.Now.AddMinutes(p[0].expires_after), 0, 0, guididx, (int)3, prn, i["name"].ToString(), p[0].idx, 0);
                            //LogManager.GetLogger("Shop.purchase").WarnFormat("Access granted for '{0}' to {1}", mgr.UserLogin, name);
                        }
                    }
                }
            }

            else if ("test" == type)
            {
                string nme = get_test_name_by_id_((System.Data.SqlClient.SqlConnection)conn, null, idx);
                sh_sit.Addshop_sold_itemRow(idx, DateTime.Now, DateTime.Now.AddMinutes(p[0].expires_after), 0, 0, guididx, (int)2, prnt_idx, nme, p[0].idx, payed);

                name = "Test: " + nme;
                mgr.check_function_and_group(s, name);
                mgr.grant_access_to_function(s);
            }
            else if ("download" == type)
            {
                string nme = get_download_name_by_id_((System.Data.SqlClient.SqlConnection)conn, null, idx);
                sh_sit.Addshop_sold_itemRow(idx, DateTime.Now, DateTime.Now.AddMinutes(p[0].expires_after), 0, 0, guididx, (int)3, prnt_idx, nme, p[0].idx, payed);
                name = "Download: " + nme;
                mgr.check_function_and_group(s, name);
                mgr.grant_access_to_function(s);
            }
            else
            {
                throw new System.Exception(String.Format("Unknown type: '{0}'", type));
            }

            sh_sit_ta.Update(sh_sit);
        }