Example #1
0
        // TODO: asAdmin
        public object callSvc(string ac, JsObject param = null, JsObject postParam = null, CallSvcOpt opt = null)
        {
            Match  m          = Regex.Match(ac, @"(\w+)(?:\.(\w+))?$");
            string ac1        = null;
            string table      = null;
            string clsName    = null;
            string methodName = null;

            if (m.Groups[2].Length > 0)
            {
                table      = m.Groups[1].Value;
                ac1        = m.Groups[2].Value;
                clsName    = onCreateAC(table);
                methodName = "api_" + ac1;
            }
            else
            {
                clsName    = "Global";
                methodName = "api_" + m.Groups[1].Value;
            }

            JDApiBase api = null;
            Assembly  asm = JDEnvBase.getAsmembly();

            api = asm.CreateInstance("JDApi." + clsName) as JDApiBase;
            if (api == null)
            {
                if (table == null)
                {
                    throw new MyException(JDApiBase.E_PARAM, "bad ac=`" + ac + "` (no Global)");
                }

                int code = !this.api.hasPerm(JDApiBase.AUTH_LOGIN)? JDApiBase.E_NOAUTH : JDApiBase.E_FORBIDDEN;
                throw new MyException(code, string.Format("Operation is not allowed for current user on object `{0}`", table));
            }
            Type       t  = api.GetType();
            MethodInfo mi = t.GetMethod(methodName);

            if (mi == null)
            {
                throw new MyException(JDApiBase.E_PARAM, "bad ac=`" + ac + "` (no method)");
            }
            api.env = this;

            NameValueCollection[] bak = null;
            if (opt != null)
            {
                if (opt.backupEnv)
                {
                    bak = new NameValueCollection[] { this._GET, this._POST }
                }
                ;
                if (opt.isCleanCall)
                {
                    this._GET  = new NameValueCollection();
                    this._POST = new NameValueCollection();
                }
            }
            if (param != null)
            {
                foreach (var kv in param)
                {
                    this._GET[kv.Key] = kv.Value.ToString();
                }
            }
            if (postParam != null)
            {
                foreach (var kv in postParam)
                {
                    this._POST[kv.Key] = kv.Value.ToString();
                }
            }

            object ret = null;

            if (clsName == "Global")
            {
                ret = mi.Invoke(api, null);
            }
            else if (t.IsSubclassOf(typeof(AccessControl)))
            {
                AccessControl accessCtl = api as AccessControl;
                accessCtl.init(table, ac1);
                accessCtl.before();
                object rv = mi.Invoke(api, null);
                //ret[1] = t.InvokeMember(methodName, BindingFlags.InvokeMethod, null, api, null);
                accessCtl.after(ref rv);
                ret = rv;
            }
            else
            {
                throw new MyException(JDApiBase.E_SERVER, "misconfigured ac=`" + ac + "`");
            }
            if (ret == null)
            {
                ret = "OK";
            }
            if (bak != null)
            {
                this._GET  = bak[0] as NameValueCollection;
                this._POST = bak[1] as NameValueCollection;
            }
            return(ret);
        }