internal WSAccessKeyEntity GenerateWSAccessKey() { WSAccessKeyEntity key = null; if (Request.Security.IsLogged && Request.Security.AuthToken.User.role >= WSConstants.ACCESS_LEVEL.ADMIN && Request.INPUT.Any(x => WSConstants.ALIACES.USER_ID.Match(x.Key))) { using (WSDataContext DBContext = GetInternalContext(Request.Meta.DB, Request.ID, $"{GetType().Name}.GenerateWSAccessKey()")) { string UserID = Request.INPUT.ReadValue(WSConstants.ALIACES.USER_ID, out UserID) ? UserID : null; Func <Type, bool> userFunc = a => a.Name.Equals("User"); PropertyInfo sessionProp = /*Request.*/ DBContext .GetType() .GetProperties() .FirstOrDefault(x => x.PropertyType.GetGenericTypeArguments().Any(userFunc)); Type userType = sessionProp == null ? null : sessionProp .PropertyType .GetGenericTypeArguments() .FirstOrDefault(userFunc); if (userType != null) { System.Reflection.MethodInfo mInfo = /*Request.*/ DBContext.GetType().GetMethod("GetTable", new Type[] { }); var UObj = mInfo.MakeGenericMethod(new Type[] { userType }).Invoke(/*Request.*/ DBContext, new object[] { }); Func <WSDynamicEntity, bool> func = s => s.getIdentities(ClientFunctions).FirstOrDefault(i => i.Key.ToLower().Equals("userid")).Value.ToString().ToLower().Equals(UserID.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)); WSDynamicEntity user = (WSDynamicEntity)method.Invoke(null, new object[] { UObj, func }); if (user != null) { object pass = null; if (user.TryReadPropertyValue("Password", out pass)) { key = new WSAccessKeyEntity(UserID.ToLower(), Request.Security.generateKey(new string[] { UserID.ToLower(), pass.ToString() })); } } } } } return(key); }
private WSSession ReadWSSession(string dbName) { WSSession session = null; if (!string.IsNullOrEmpty(dbName)) { try { WSSecurityMeta meta = SecurityMap[dbName]; session = new WSSession(Request.SessionID, meta); /********************** * ANDVO@NOTE: * DO NOT do [ZoneContext = Request.ZoneContext;] * because it will use Request's zone, when it MUST BE zone for the argument:'dbName' !!! * */ using (WSDataContext ZoneContext = GetInternalContext(meta.Zone, Request.ID, $"{GetType().Name}.ReadWSSession('{dbName}')")) { if (ZoneContext != null && !ZoneContext.IsDisposed && ZoneContext.Connection.State == System.Data.ConnectionState.Open) { if (meta.SessionType != null) { 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(Request.SessionID.ToLower()); MethodInfo[] methods = typeof(Enumerable).GetMethods(BindingFlags.Static | BindingFlags.Public); var method = methods.FirstOrDefault(m => m.Name == "FirstOrDefault" && m.GetParameters().Count() == 2).MakeGenericMethod(typeof(WSDynamicEntity)); WSDynamicEntity sessionEntity = (WSDynamicEntity)method.Invoke(null, new object[] { tObj, func }); if (sessionEntity != null) { WSDynamicEntity userEntity = (WSDynamicEntity)sessionEntity.GetType().GetProperties().Single(x => x.PropertyType == meta.UserType).GetValue(sessionEntity, null); if (userEntity != null) { object _id = userEntity.TryReadPropertyValue("UserID", out _id) ? _id : null; object _email = userEntity.TryReadPropertyValue("Email", out _email) ? _email : null; object _firstname = userEntity.TryReadPropertyValue("FirstName", out _firstname) ? _firstname : null; object _lastname = userEntity.TryReadPropertyValue("LastName", out _lastname) ? _lastname : null; object _isactive = userEntity.TryReadPropertyValue("IsActive", out _isactive) ? _isactive : null; object _login = userEntity.TryReadPropertyValue("Login", out _login) ? _login : null; WSDynamicEntity roleEntity = (WSDynamicEntity)userEntity.GetType().GetProperties().FirstOrDefault(x => x.PropertyType == meta.RoleType).GetValue(userEntity, null); if (roleEntity != null) { object _role = roleEntity.TryReadPropertyValue("ID", out _role) ? _role : null; object _roleName = roleEntity.TryReadPropertyValue("Name", out _roleName) ? _roleName : null; int id = int.TryParse(_id.ToString(), out id) ? id : -1; string email = _email != null?_email.ToString() : string.Empty; string login = _login != null?_login.ToString() : string.Empty; string firstname = _firstname != null?_firstname.ToString() : string.Empty; string lastname = _lastname != null?_lastname.ToString() : string.Empty; bool isactive = bool.TryParse(_isactive.ToString(), out isactive) ? isactive : false; byte role = byte.TryParse(_role == null ? null : _role.ToString(), out role) ? role : WSConstants.DEFAULT_USER_ROLE; string roleName = _roleName != null?_roleName.ToString() : string.Empty; session.user = new WSUserToken() { id = id, email = email, login = login, firstname = firstname, lastname = lastname, isactive = isactive, role = role, roleName = roleName }; } } } } } } } catch (Exception e) { RegError(GetType(), e, ref LoadStatus); } } return(session); }
public bool TrySetRecordValue(string colName, object newValue, WSDataContext DBContext, MetaFunctions CFunc, Func <Exception, bool> AddError = null) { bool done = false; if (!string.IsNullOrEmpty(colName)) { PropertyInfo pInfo = GetType().GetProperty(colName); if (pInfo != null) { object orgValue = pInfo.GetValue(this, null); newValue = fixSpecialCaseValue(colName, newValue, pInfo.PropertyType); try { if ( orgValue == newValue || (orgValue == null && newValue == null) || (orgValue != null && newValue != null && orgValue.ToString().Equals(newValue.ToString())) ) { return(true); } else { if (newValue == null) { if (pInfo.PropertyType.IsNullable()) { pInfo.SetPropertyValue(this, null); done = true; } } else { object newValueConverted = pInfo.PropertyType.IsAssignableFrom(newValue.GetType()) ? newValue : null; if (newValueConverted != null || pInfo.PropertyType.Read(newValue, out newValueConverted, null, null, pInfo.Name)) { try { bool IsAssiciation = false; PropertyInfo association = null; WSTableSource associationSrc = null; WSTableParam associationKey = null; PropertyInfo[] props = GetType().GetProperties(); foreach (PropertyInfo prop in props) { IEnumerable <CustomAttributeData> cAttrs = prop.CustomAttributesData(); foreach (CustomAttributeData cad in cAttrs) { CustomAttributeNamedArgument IsForeignKey = cad.NamedArguments.FirstOrDefault(x => x.MemberInfo.Name.Equals("IsForeignKey")); if (IsForeignKey != null && IsForeignKey.TypedValue.Value != null && (true.ToString()).Equals(IsForeignKey.TypedValue.Value.ToString())) { CustomAttributeNamedArgument cana = cad.NamedArguments.FirstOrDefault(x => x.MemberInfo.Name.Equals("ThisKey")); if (cana != null && pInfo.Name.Equals(cana.TypedValue.Value == null ? null : cana.TypedValue.Value.ToString())) { CustomAttributeNamedArgument canaKey = cad.NamedArguments.FirstOrDefault(x => x.MemberInfo.Name.Equals("OtherKey")); if (canaKey != null && canaKey.TypedValue.Value != null) { IsAssiciation = true; association = prop; associationSrc = (WSTableSource)CFunc.GetSourceByType(association.PropertyType); associationKey = (WSTableParam)associationSrc.GetXParam(canaKey.TypedValue.Value.ToString()); } } } } } if (IsAssiciation) { string pName = pInfo.Name; ParameterExpression paramExp = Expression.Parameter(association.PropertyType, "x"); WSCombineFilter filter = new WSCombineFilter(WSCombineFilter.SQLMode.AndAlso); filter.Save(new WSJValue(newValueConverted.ToString()).GetFieldFilter(CFunc, associationKey, paramExp, 0)); object subExpr = (Expression)filter.GetType().GetMethod("ToLambda").MakeGenericMethod(new Type[] { association.PropertyType }).Invoke(filter, new object[] { paramExp }); MethodInfo mInfo = DBContext.GetType().GetMethod("GetTable", new Type[] { }); var tObj = mInfo.MakeGenericMethod(new Type[] { association.PropertyType }).Invoke(DBContext, new object[] { }); Func <WSDynamicEntity, bool> func = s => s.readPropertyValue(associationKey.WSColumnRef.NAME).ToString().Equals(newValueConverted.ToString()); var method = typeof(Enumerable).GetMethods(System.Reflection.BindingFlags.Static | System.Reflection.BindingFlags.Public) .FirstOrDefault(m => m.Name == "FirstOrDefault" && m.GetParameters().Count() == 2).MakeGenericMethod(typeof(WSDynamicEntity)); WSDynamicEntity newAssociation = (WSDynamicEntity)method.Invoke(null, new object[] { tObj, func }); if (newAssociation != null) { association.SetPropertyValue(this, newAssociation); done = true; } } else { pInfo.SetPropertyValue(this, newValueConverted); done = true; } } catch (Exception e1) { AddError(e1); } } } } } catch (Exception e) { AddError(e); } if (!done) { pInfo.SetPropertyValue(this, orgValue); } } } return(done); }
private static WSSources <WSTableSource> LoadMetaSources() { LoadStatusStatic.AddNote($"IOSB.LoadMetaSources()"); WSSources <WSTableSource> ORG_SOURCES = new WSSources <WSTableSource>(); Dictionary <Type, WSDataContext> dbList = new Dictionary <Type, WSDataContext>(); try { if (EntityTypes != null && EntityTypes.Any()) { IEnumerable <string> namespaces_ = EntityTypes.Select(t => t.Namespace).Distinct(); Dictionary <string, List <Type> > nsList = namespaces_.ToDictionary(key => key, val => new List <Type>()); foreach (Type type in EntityTypes) { nsList[type.Namespace].Add(type); } foreach (KeyValuePair <string, List <Type> > ns in nsList) { if (ns.Value.Any()) { foreach (Type type in ns.Value) { try { Type DCType = GetDCTypeByEntityType(type); WSDataContext db = null; if (dbList.Any(x => x.Key == DCType)) { db = dbList.FirstOrDefault(x => x.Key == DCType).Value; } else { db = GetServerContext(DCType, null, $"{typeof(WSServerMeta).Name}.LoadMetaSources() => [{type.FullName}:{DCType.Name}"); dbList.Add(DCType, db); } if (db != null) { string DBName = db.GetType().CustomAttribute <DatabaseAttribute>(true).Name; WSTableSource tSrc = new WSTableSource( type, SecurityMap.FirstOrDefault(m => m.Key.Equals(DBName)).Value.Zone, type.Name, ServerFunctions, WSConstants.ACCESS_LEVEL.READ ); #region READ PROPERTIES List <MetaDataMember> eProps = db.ReadProperties(type, ref LoadStatusStatic); if (eProps != null && eProps.Any()) { List <WSTableParam> _params = new List <WSTableParam>(); foreach (MetaDataMember prop in eProps) { try { WSTableParam tParam = new WSTableParam(type, next_code, prop.Name, new WSColumnRef(prop.Name), prop.Type, ServerFunctions); object[] CustomAttributes = prop.Member.GetCustomAttributes(true); IEnumerable <AssociationAttribute> assAttributes = CustomAttributes.OfType <AssociationAttribute>(); if (assAttributes != null && assAttributes.Any()) { tParam.IsAssociation = true; } IEnumerable <ColumnAttribute> cAttributes = CustomAttributes.OfType <ColumnAttribute>(); if (cAttributes != null && cAttributes.Any()) { tParam.IsColumn = true; if (cAttributes.FirstOrDefault().IsPrimaryKey) { tParam.WRITE_ACCESS_MODE = new WSAccessMode(WSConstants.ACCESS_LEVEL.LOCK, false); if (!tSrc.Params.Any(p => p.Match(WSConstants.PARAMS.RECORD_ID.NAME))) { tParam.DISPLAY_NAME = WSConstants.PARAMS.RECORD_ID.NAME; if (!tParam.ALIACES.Any(a => a.Equals(WSConstants.PARAMS.RECORD_ID.NAME))) { tParam.ALIACES.Add(WSConstants.PARAMS.RECORD_ID.NAME); } } } } _params.Add(tParam); } catch (Exception) { } } tSrc.AddParams(_params); tSrc.ClearDublicatedAliaces(); } #endregion if (!ORG_SOURCES.Any(x => x.Match(tSrc))) { ORG_SOURCES.Add(tSrc); } } } catch (Exception) { } } } } } } catch (Exception) { } finally { foreach (Type t in dbList.Keys) { try { if (dbList[t] != null) { dbList[t].Dispose(); } } catch (Exception e) { } } } return(ORG_SOURCES); }