Esempio n. 1
0
        public void EnterPrivateSession <S>(WSDataContext db, ClientFunctions CFunc, WSTableSource SessionSrc, WSUserToken _user, string _1MinTicket, ref WSStatus statusLines, bool renew = false) where S : WSDynamicEntity
        {
            try
            {
                if (db != null)
                {
                    if (ExitPrivateSession <S>(db, CFunc, SessionSrc, ref statusLines) && renew && _user.IsValid && ValidateOneMinTicket(_1MinTicket))
                    {
                        try
                        {
                            AuthToken.User = _user;

                            S _session = (S)Activator.CreateInstance(typeof(S), new object[] { });

                            setSession(DateTime.Now, SessionID, AuthToken.User.id, ref _session);

                            db.GetTable <S>().InsertOnSubmit(_session);

                            db.SubmitChanges();

                            Func <S, bool> func = s => s.readPropertyValue(WSConstants.PARAMS.SESSIONID.NAME, "").ToString().ToLower().Equals(SessionID.ToLower());

                            Session = db.GetTable <S>().FirstOrDefault(func);
                        }
                        catch (Exception e) { CFunc.RegError(GetType(), e, ref statusLines); Session = null; }
                    }
                }

                if (Session != null)
                {
                    setAuthToken(Session, ref _AuthToken);
                }
            }
            catch (Exception e) { CFunc.RegError(GetType(), e, ref statusLines); }
        }
Esempio n. 2
0
        private WSDynamicEntity ReadSession(/*WSDataContext ZoneContext*/)
        {
            WSDynamicEntity entity = null;

            if (Meta != null && !string.IsNullOrEmpty(SessionID) && ZoneContext != null)
            {
                long init_ticks = DateTime.Now.Ticks;
                try
                {
                    status.AddNote("Zone:" + Meta.Zone, WSConstants.ACCESS_LEVEL.READ);

                    System.Reflection.MethodInfo mInfo = ZoneContext.GetType().GetMethod("GetTable", new Type[] { });

                    var tObj = mInfo.MakeGenericMethod(new Type[] { Meta.SessionType }).Invoke(ZoneContext, new object[] { });

                    Func <WSDynamicEntity, bool> func = s => s.readPropertyValue(WSConstants.PARAMS.SESSIONID.NAME, "").ToString().ToLower().Equals(SessionID.ToLower());

                    System.Reflection.MethodInfo[] methods = typeof(Enumerable).GetMethods(System.Reflection.BindingFlags.Static | System.Reflection.BindingFlags.Public);

                    var method = methods.FirstOrDefault(m => m.Name == "FirstOrDefault" && m.GetParameters().Count() == 2).MakeGenericMethod(typeof(WSDynamicEntity));

                    entity = (WSDynamicEntity)method.Invoke(null, new object[] { tObj, func });
                }
                catch (Exception e) { CFunc.RegError(GetType(), e, ref status); }

                //if (ZoneContext != null) ZoneContext.Close(/*SessionID*/);

                TimeSpan ticks1 = new TimeSpan(DateTime.Now.Ticks - init_ticks); init_ticks = DateTime.Now.Ticks;
            }
            return(entity);
        }
Esempio n. 3
0
        public bool ExitPrivateSession <S>(WSDataContext db, ClientFunctions CFunc, WSTableSource SessionSrc, ref WSStatus statusLines) where S : WSDynamicEntity
        {
            bool DEAUTHORIZED = false;

            try
            {
                AuthToken.issued     = DateTime.MinValue;
                AuthToken.expires    = DateTime.MinValue;
                AuthToken.expires_in = 0;
                AuthToken.User       = null;

                if (Session == null)
                {
                    DEAUTHORIZED = true;
                }
                else
                {
                    if (db != null)
                    {
                        //TODO@ANDVO:2016-11-09 : instead of looking for Primary Key value, - look for 'SessionID' field to make sure ALL related records will be removed

                        string idName  = SessionSrc.PrimParams.Any() && SessionSrc.PrimParams.Count() == 1 ? SessionSrc.PrimParams.Single().WSColumnRef.NAME : null;
                        object idValue = null;

                        if (Session.TryReadPropertyValue(idName, out idValue))
                        {
                            ParameterExpression paramExp = Expression.Parameter(SessionSrc.ReturnType, "x");

                            Expression <Func <S, bool> > expr = new WSJValue(idValue.ToString()).GetFieldFilter(CFunc, (WSTableParam)SessionSrc.GetXParam(idName), paramExp, 0).ToLambda <S>(paramExp);

                            S delItem = db.GetTable <S>().FirstOrDefault(expr);

                            db.GetTable <S>().DeleteOnSubmit(delItem);
                            db.SubmitChanges();
                            DEAUTHORIZED = true;
                        }
                    }
                }
                if (DEAUTHORIZED)
                {
                    AuthToken.status = WSConstants.AUTH_STATES.DEAUTHORIZED;
                }
            }
            catch (Exception e)
            {
                CFunc.RegError(GetType(), e, ref statusLines);
                AuthToken.status = WSConstants.AUTH_STATES.FAILED_DEAUTHORIZE;
            }
            finally { WSServerMeta.ClearCache(SessionID); }
            return(DEAUTHORIZED);
        }
Esempio n. 4
0
        private bool Match(ClientFunctions CFunc, WSDynamicEntity refEntity)
        {
            try
            {
                if (refEntity == null)
                {
                    return(false);
                }
                else if (refEntity.GetType() != GetType())
                {
                    return(false);
                }
                else
                {
                    Type          orgType = GetType();
                    WSTableSource orgSrc  = (WSTableSource)getSource(CFunc);
                    IEnumerable <WSTableParam> orgParams = orgSrc.DBParams.Where(p => p.DataType.IsSimple());

                    Type          refType = refEntity.GetType();
                    WSTableSource refSrc  = ((WSTableSource)CFunc.GetSourceByType(refType));
                    IEnumerable <WSTableParam> refParams = refSrc.DBParams.Where(p => p.DataType.IsSimple());

                    IEnumerable <WSTableParam> orgExceptParams = orgParams.Where(p1 => !refParams.Any(p2 => p2.Match(p1)));
                    IEnumerable <WSTableParam> refExceptParams = refParams.Where(p1 => !orgParams.Any(p2 => p2.Match(p1)));

                    if (orgExceptParams.Any() || refExceptParams.Any())
                    {
                        return(false);
                    }
                    else
                    {
                        foreach (WSTableParam param in orgParams)
                        {
                            object orgInfo = orgType.GetProperties().FirstOrDefault(p => p.Name.Equals(param.WSColumnRef.NAME)).GetValue(this, null);
                            object refInfo = refType.GetProperties().FirstOrDefault(p => p.Name.Equals(param.WSColumnRef.NAME)).GetValue(refEntity, null);
                            if (!(orgInfo == null && refInfo == null) && !orgInfo.ToString().Equals(refInfo.ToString()))
                            {
                                return(false);
                            }
                        }
                        return(true);
                    }
                }
            }
            catch (Exception e) { WSStatus status = WSStatus.NONE.clone(); CFunc.RegError(GetType(), e, ref status, $"Match():321"); }
            return(false);
        }
Esempio n. 5
0
        public bool IsRelationTo(ClientFunctions CFunc, WSDynamicEntity _RelationTEntity, ref WSStatus _statusLines, IEnumerable <Type> _refTypes = null)
        {
            bool _IsRelationTo = false;

            try
            {
                if (_RelationTEntity != null)
                {
                    WSTableSource orgSrc = (WSTableSource)getSource(CFunc);
                    WSTableSource relSrc = (WSTableSource)_RelationTEntity.getSource(CFunc);
                    if (relSrc != null)
                    {
                        WSTableParam refParam = orgSrc.DBParams.FirstOrDefault(x => x.DataType == relSrc.ReturnType);
                        if (refParam == null)
                        {
                            IEnumerable <Type>         refTypes = _refTypes == null ? new List <Type>() : _refTypes.Select(x => x);
                            IEnumerable <WSTableParam> eParams  = orgSrc.DBParams.Any() ? orgSrc.DBParams.Where(x => x.DataType.IsValidDynamicEntity() && !x.DataType.IsCollection() && !refTypes.Any(t => t == x.DataType)) : null;
                            if (eParams != null && eParams.Any())
                            {
                                foreach (WSTableParam eParam in eParams)
                                {
                                    object          pValue  = null;
                                    WSDynamicEntity pEntity = TryReadPropertyValue(eParam.WSColumnRef.NAME, out pValue, null) ? (WSDynamicEntity)pValue : null;

                                    _IsRelationTo = pEntity.IsRelationTo(CFunc, _RelationTEntity, ref _statusLines, refTypes);

                                    if (_IsRelationTo)
                                    {
                                        break;
                                    }
                                }
                            }
                        }
                        else
                        {
                            object          PValue    = null;
                            WSDynamicEntity RefEntity = TryReadPropertyValue(refParam.WSColumnRef.NAME, out PValue, null) ? (WSDynamicEntity)PValue : null;

                            _IsRelationTo = RefEntity != null && _RelationTEntity.Match(CFunc, RefEntity);
                        }
                    }
                }
            }
            catch (Exception e) { CFunc.RegError(GetType(), e, ref _statusLines, $"IsRelationTo():256"); }
            return(_IsRelationTo);
        }
Esempio n. 6
0
        public override void WriteJson(JsonWriter writer, object value, JsonSerializer serializer)
        {
            WSStatus status = WSStatus.NONE_Copy();

            try
            {
                if (value != null)
                {
                    if (value is IList)
                    {
                        serializeWSRecords(writer, serializer, (IList)value, XParams);
                    }
                    else if (value is WSRecord)
                    {
                        ((WSRecord)value).WriteJson(writer, serializer, new List <Type>(), Request, CFunc, DBContext);
                    }
                }
            }
            catch (Exception e) { CFunc.RegError(GetType(), e, ref status); }
        }
Esempio n. 7
0
        public WSDynamicEntity getRelatedParent <A>(ClientFunctions CFunc, ref WSStatus _statusLines, IEnumerable <Type> _refTypes = null)
        {
            WSDynamicEntity relEntity = null;

            try
            {
                WSTableSource orgSrc = (WSTableSource)getSource(CFunc);
                WSTableSource relSrc = (WSTableSource)CFunc.GetSourceByType(typeof(A));
                if (relSrc != null)
                {
                    WSTableParam refParam = orgSrc.DBParams.FirstOrDefault(x => x.DataType == relSrc.ReturnType);
                    if (refParam == null)
                    {
                        IEnumerable <Type>         refTypes = _refTypes == null ? new List <Type>() : _refTypes.Select(x => x);
                        IEnumerable <WSTableParam> eParams  = orgSrc.DBParams.Any() ? orgSrc.DBParams.Where(x => x.DataType.IsValidDynamicEntity() && !x.DataType.IsCollection() && !refTypes.Any(t => t == x.DataType)) : null;
                        if (eParams != null && eParams.Any())
                        {
                            foreach (WSTableParam eParam in eParams)
                            {
                                object          pValue  = null;
                                WSDynamicEntity pEntity = TryReadPropertyValue(eParam.WSColumnRef.NAME, out pValue, null) ? (WSDynamicEntity)pValue : null;

                                relEntity = pEntity.getRelatedParent <A>(CFunc, ref _statusLines, refTypes);

                                if (relEntity != null)
                                {
                                    break;
                                }
                            }
                        }
                    }
                    else
                    {
                        object PValue = null;
                        relEntity = TryReadPropertyValue(refParam.WSColumnRef.NAME, out PValue, null) ? (WSDynamicEntity)PValue : null;
                    }
                }
            }
            catch (Exception e) { CFunc.RegError(GetType(), e, ref _statusLines, $"getRelatedParent():211"); }
            return(relEntity);
        }