internal void Configure(WSSource Source, WSJson json) { try { if (Source != null && json != null) { if (json is WSJProperty && ((WSJProperty)json).Value is WSJValue) { WSJValue vJson = (WSJValue)((WSJProperty)json).Value; switch (((WSJProperty)json).Key) { case "readaccess": byte readAccessValue = byte.TryParse(vJson.Value, out readAccessValue) ? readAccessValue : READ_ACCESS_MODE.ACCESS_LEVEL; READ_ACCESS_MODE = new WSAccessMode(readAccessValue, READ_ACCESS_MODE.OWNER_ACCESS_ALLOWED); break; case "writeaccess": byte writeAccessValue = byte.TryParse(vJson.Value, out writeAccessValue) ? writeAccessValue : WRITE_ACCESS_MODE.ACCESS_LEVEL; WRITE_ACCESS_MODE = new WSAccessMode(writeAccessValue, WRITE_ACCESS_MODE.OWNER_ACCESS_ALLOWED); break; case "skipempty": bool skipempty = vJson.Value.IsTrue() ? true : vJson.Value.IsFalse() ? false : SkipEmpty; SkipEmpty = skipempty; break; default: break; } } } } catch (Exception) { } }
private bool validateType(JsonWriter writer, WSParam xParam, object obj, List <Type> printedTypes, bool ignoreCurcularity, WSRequest Request, MetaFunctions CFunc) { bool isValid = false; if (obj != null && Request.Security != null && Request.Security.IsValid && xParam != null) { byte uRole = Request.Security.IsLogged ? Request.Security.AuthToken.User.role : WSConstants.ACCESS_LEVEL.READ; int paramAccessLevel = xParam.READ_ACCESS_MODE.ACCESS_LEVEL; WSTableSource xSource = (WSTableSource)CFunc.GetSourceByType(obj.GetType()); paramAccessLevel = (xSource != null && xSource.AccessLevel > paramAccessLevel) ? xSource.AccessLevel : paramAccessLevel; bool showCycledTypesMessage = uRole >= WSConstants.ACCESS_LEVEL.ADMIN && xSource != null && xSource.ShowMessageInaccessible; Type entityType = null; if (obj.IsCollection()) { if (obj.IsCollectionOf <WSEntity>()) { entityType = (obj as IList).GetEntityType(); } else if (obj.IsCollectionOf <WSRecord>() && (obj as IList).Count > 0) { entityType = ((WSRecord)(obj as IList)[0]).entity.GetType(); } } else if (obj is WSRecord) { entityType = ((WSRecord)obj).entity.GetType(); } else if (obj is WSEntity) { entityType = obj.GetType(); } bool Ignore = ignoreCurcularity ? false : (printedTypes != null && printedTypes.Any(t => t == entityType)); if (Ignore) { if (showCycledTypesMessage) { WritePropName(writer, xParam.NAME); writer.WriteValue("[unavailable : cycled references detected]"); } } else { if (entityType.IsValidDynamicEntity()) { WSSource src = CFunc.GetSourceByType(entityType); isValid = src != null && uRole >= src.AccessLevel; } else { isValid = true; } } } return(isValid); }
public WSSource getSource(MetaFunctions CFunc) { if (_xSource == null) { _xSource = CFunc.GetSourceByType(GetType()); } return(_xSource); }
internal WSSource Clone(MetaFunctions _CFunc, byte?_UserRole = null, bool _IsBase = false) { WSSource src = new WSSource(ReturnType, _CFunc == null ? CFunc : _CFunc, ALIACES, NAME, _UserRole == null ? UserRole : (byte)_UserRole, _IsBase, AccessLevel) { PrintStatus = PrintStatus, ShowMessageInaccessible = ShowMessageInaccessible, DefaultSchemaDeepness = DefaultSchemaDeepness, Params = Params.Where(x => x != null).Select(x => x is WSTableParam ? ((WSTableParam)x).Clone() : x.Clone()) }; return(src); }
private WSParam GetParam(WSSource xSource, string name, Type DefaultFieldType = null) { WSParam xParam = null; if (this is WSDynamicEntity && xSource != null) { xParam = xSource.GetXParam(name, DefaultFieldType); } else if (this is WSStaticEntity || this is WSV1ResponseEntity) { xParam = new WSParam(0, name, DefaultFieldType, null); } return(xParam); }
public void SaveStaticSource(WSSource src) { try { if (!STATIC_SOURCES.Any(s => s.Match(src))) { STATIC_SOURCES.Add(src); } else { STATIC_SOURCES.FirstOrDefault(x => x.Match(src)).Merge(src); } } catch (Exception e) { RegError(GetType(), e, ref LoadStatus); } }
private string LoadJson() { StringBuilder sb = new StringBuilder(); sb.Append("\"Name\":\"" + ReturnType.Name + "\""); sb.Append(",\"ReturnType\":\"" + ReturnType.FullName + "\""); sb.Append(",\"SecurityZone\":\"" + SecurityZone + "\""); sb.Append("," + base.Json); sb.Append(",\"Params\":["); Func <WSParam, bool> isAccessibleParam = x => x.READ_ACCESS_MODE.ACCESS_LEVEL <= UserRole; Func <WSParam, string> jFunc = (x => "{" + (x is WSTableParam ? ((WSTableParam)x).Json : x.Json) + "}"); if (Params != null && Params.Any(isAccessibleParam)) { List <string> paramList = new List <string>(); foreach (WSParam param in Params.Where(isAccessibleParam)) { if (param is WSTableParam) { WSTableParam tParam = (WSTableParam)param; if (tParam.IsAssociation) { WSSource src = CFunc.GetSourceByType(tParam.DataType.GetEntityType()); if (src != null && src.AccessLevel <= UserRole) { paramList.Add("{" + tParam.Json + "}"); } } else { paramList.Add("{" + tParam.Json + "}"); } } else { paramList.Add("{" + param.Json + "}"); } } sb.Append(paramList.Aggregate((a, b) => a + "," + b)); } sb.Append("]"); return(sb.ToString()); }
public void Merge(WSSource ext) { base.Merge(ext); PrintStatus = ext.PrintStatus; ShowMessageInaccessible = ext.ShowMessageInaccessible; DefaultSchemaDeepness = ext.DefaultSchemaDeepness; if (WSConstants.ACCESS_LEVEL.LEVELS.Any(x => x == ext.AccessLevel)) { AccessLevel_ = ext.AccessLevel; } foreach (WSParam orgParam in Params.OfType <WSParam>()) { WSParam extParam = ext.Params.FirstOrDefault(p => p.Match(orgParam.NAME)); if (extParam != null) { orgParam.Merge(extParam); } } }
private WSSource GetSource(string SRCName, string DBName = null) { WSSource src = null; try { Func <WSSource, bool> sFunc = s => s.Match(SRCName); src = STATIC_SOURCES.Any(sFunc) ? STATIC_SOURCES.Single(sFunc) : null; if (src == null && !string.IsNullOrEmpty(DBName) && DYNAMIC_SOURCES != null && DYNAMIC_SOURCES.ContainsKey(DBName)) { Func <WSTableSource, bool> dFunc = x => x.Match(SRCName, DBName); src = DYNAMIC_SOURCES[DBName].Any(dFunc) ? DYNAMIC_SOURCES[DBName].Single(dFunc) : null; } if (src == null) { //...throw error } } catch (Exception e) { RegError(GetType(), e, ref LoadStatus); } return(src); }
public WSSources <T> Clone(ref WSUserDBSet DBSet, MetaFunctions Func) { WSSources <T> srcs = new WSSources <T>(); try { foreach (T src in this) { if (src is WSTableSource) { WSSource tSrc = DBSet == null ? (src as WSTableSource).Clone(Func) : (src as WSTableSource).Clone(Func, DBSet.DBSession.user.role); srcs.Add((T)tSrc); } else { srcs.Add((src as WSSource).Clone(Func, DBSet.DBSession.user.role, false) as T); } } } catch (Exception) { } return(srcs); }
public bool Match(WSSource match) { return(match != null && Match(match.ReturnType)); }
public WSStatus WriteJson(JsonWriter writer, JsonSerializer serializer, WSSchema schema, WSParamList outFields, List <Type> printedTypes, WSRequest Request, MetaFunctions CFunc, WSDataContext DBContext) { WSStatus status = WSStatus.NONE_Copy(); WSEntitySchema eSchema = null; WSSource xSource = CFunc.GetSourceByType(GetType()); if (schema != null && schema is WSEntityBaseSchema) { if (schema is WSEntitySchema) { eSchema = (WSEntitySchema)schema; } else if (schema is WSEntityListSchema) { eSchema = ((WSEntityListSchema)schema).EntitySchema; } } if (eSchema == null && this is WSDynamicEntity) { if (xSource != null && xSource is WSTableSource && ((WSTableSource)xSource).BaseSchema != null) { eSchema = ((WSTableSource)xSource).BaseSchema; } } #region Read if in 'COLLAPSED' mode bool collapsedMode = false; if (eSchema != null) { IEnumerable <WSJObject> _JOptions = eSchema.IOBaseOptions.Value.OfType <WSJObject>(); Func <WSJProperty, bool> func = v => WSConstants.ALIACES.OPTION_COLLECT.Match(v.Key); WSJObject takeJOption = _JOptions.FirstOrDefault(x => x.Value.Any(func)); if (takeJOption != null && takeJOption.IsValid) { collapsedMode = true; status.childs.Add(WriteCollapsedValues(writer, serializer, this, xSource, takeJOption.Value.FirstOrDefault(func).Value, Request, CFunc)); } } #endregion if (!collapsedMode) { writer.WriteStartObject(); if (this is WSStaticEntity || (eSchema != null && eSchema.Fields.Any())) { List <Type> postPrintedTypes = printedTypes.Select(x => x).ToList(); if (!postPrintedTypes.Any(x => x == GetType())) { postPrintedTypes.Add(GetType()); } status.childs.Add( WriteJMembers( GetType().GetMembers(BindingFlags.Public | BindingFlags.Instance).Where(m => m is PropertyInfo || m is FieldInfo), writer, serializer, eSchema, xSource, outFields, printedTypes, Request, CFunc, DBContext ) ); } writer.WriteEndObject(); } return(status); }
private WSStatus WriteCollapsedValues(JsonWriter writer, JsonSerializer serializer, WSEntity entity, WSSource xSource, WSJson collapseOption, WSRequest Request, MetaFunctions CFunc) { WSStatus status = WSStatus.NONE_Copy(); try { /******************************************************************************* * * // TODO @ANDVO : 2016-02-15 : IMPORTANT!!! => Implement security check like this : * * WSStatus status = Validate(obj, xParam, writer, serializer, security, schema); * if (status.CODE == WSStatus.SUCCESS.CODE) * { */ if (entity != null && collapseOption != null && collapseOption.IsValid) { WSTableSource childSource = (WSTableSource)CFunc.GetSourceByType(entity.GetType()); object fieldValue = null; WSParam field = null; if (collapseOption is WSJValue) { string fieldName = ((WSJValue)collapseOption).Value; field = entity.GetParam(xSource, fieldName); fieldValue = getMemberValue(entity, field, CFunc); WSPrimitiveFieldSchema fieldSchema = new WSPrimitiveFieldSchema(CFunc, (WSTableParam)field, new WSJProperty(fieldName, new WSJArray()), /*((WSTableSource)entity.getSource())*/ childSource.BaseSchema); if (Validate(fieldValue, field, writer, serializer, childSource.BaseSchema, childSource, null, ref status, Request, CFunc)) { object _obj = null; serializer.Serialize(writer, field.TryReadPrimitiveWithDefault(fieldValue, string.Empty, out _obj) ? _obj : string.Empty); writer.Flush(); status = WSStatus.SUCCESS_Copy(); } } else if (collapseOption is WSJObject) { WSJProperty collapseSrc = ((WSJObject)collapseOption).Value.FirstOrDefault(); field = entity.GetParam(childSource, collapseSrc.Key); fieldValue = getMemberValue(entity, field, CFunc); if (Validate(fieldValue, field, writer, serializer, childSource.BaseSchema, childSource, null, ref status, Request, CFunc)) { if (fieldValue == null) { serializer.Serialize(writer, "NULL"); writer.Flush(); } else if (fieldValue is WSEntity) { WSTableSource fieldSource = (WSTableSource)CFunc.GetSourceByType(fieldValue.GetType()); status = WriteCollapsedValues(writer, serializer, (WSEntity)fieldValue, fieldSource, collapseSrc.Value, Request, CFunc); } else if (fieldValue.IsCollectionOf <WSEntity>()) { WSTableSource fieldSource = (WSTableSource)CFunc.GetSourceByType(fieldValue.GetType().GetEntityType()); if (!((IEnumerable <WSEntity>)fieldValue).Any()) { serializer.Serialize(writer, "NULL"); writer.Flush(); } else { foreach (WSEntity eItem in (IEnumerable <WSEntity>)fieldValue) { status.childs.Add(WriteCollapsedValues(writer, serializer, eItem, fieldSource, collapseSrc.Value, Request, CFunc)); } status = status.IsPositive ? WSStatus.SUCCESS_Copy() : WSStatus.ERROR_Copy(); } } } } } /*} * *******************************************************************************/ } catch (Exception e) { CFunc.RegError(GetType(), e, ref status); } return(status); }
private WSStatus WriteJProperty(object obj, WSParam xParam, JsonWriter writer, JsonSerializer serializer, WSSchema schema, WSSource xSource, WSParamList outFields, List <Type> printedTypes, WSRequest Request, MetaFunctions CFunc, WSDataContext DBContext) { WSStatus status = WSStatus.NONE_Copy(); try { if (Validate(obj, xParam, writer, serializer, schema, xSource, outFields, ref status, Request, CFunc)) { if (obj == null) { WritePropName(writer, ((schema != null && !string.IsNullOrEmpty(schema.Name)) ? schema.Name : xParam.NAME), true, PrintMode.ValueCell); serializer.Serialize(writer, obj); } else if (obj is WSStatus || obj is WSStatus_JSON) { #region PRINT WSStatus WSStatus_JSON json = obj is WSStatus_JSON ? (WSStatus_JSON)obj : ((WSStatus)obj).GetJson(); if (json != null) { WritePropName(writer, ((schema != null && !string.IsNullOrEmpty(schema.Name)) ? schema.Name : xParam.NAME), true, PrintMode.TableHeader); serializer.Serialize(writer, json); } #endregion } else if (obj.GetType().IsSimple()) { #region PRINT PRIMITIVE FIELD if (obj is DateTime) { obj = ((DateTime)obj).ToString(WSConstants.DATE_FORMAT); } else if (obj is TimeSpan) { obj = ((TimeSpan)obj).ToString(WSConstants.TIMESPAN_FORMAT_SIMPLE); } WritePropName(writer, (schema != null && !string.IsNullOrEmpty(schema.Name)) ? schema.Name : xParam.NAME); object _obj = null; serializer.Serialize(writer, xParam.TryReadPrimitiveWithDefault(obj, string.Empty, out _obj) ? _obj : string.Empty); #endregion } else if (obj.GetType().IsSimpleCollection()) { #region PRINT PRIMITIVE COLLECTION string key = (schema != null && !string.IsNullOrEmpty(schema.Name)) ? schema.Name : xParam.NAME; status.AddNote("ready to searialize primitive fields (" + key + ")"); WritePropName(writer, key); serializer.Serialize(writer, obj); #endregion } else if (obj is WSRecord) { #region PRINT WSRecord string pKey = (schema != null && !string.IsNullOrEmpty(schema.Name)) ? schema.Name : xParam.NAME; WritePropName(writer, pKey); ((WSRecord)obj).WriteJson(writer, serializer, printedTypes, Request, CFunc, DBContext); #endregion } else if (obj.IsCollectionOf <WSRecord>()) { #region PRINT WSRecord Collection string pKey = (schema != null && !string.IsNullOrEmpty(schema.Name)) ? schema.Name : xParam.NAME; WritePropName(writer, pKey); writer.WriteStartArray(); IList list = obj as IList; foreach (WSRecord record in list) { if (record != null) { record.WriteJson(writer, serializer, printedTypes, Request, CFunc, DBContext); } } writer.WriteEndArray(); #endregion } else { #region PRINT ENTITY bool printAllowed = (this is WSStaticEntity) || ( schema is WSEntityBaseSchema && validateType(writer, xParam, obj, printedTypes, true, Request, CFunc) ); if (printAllowed) { string pKey = (schema != null && !string.IsNullOrEmpty(schema.Name)) ? schema.Name : xParam.NAME; WritePropName(writer, pKey, false); #region PRINT WSEntity if (obj is WSEntity) { if (obj is WSDynamicEntity && !((WSDynamicEntity)obj).Match(Request, DBContext, CFunc, schema)) { serializer.Serialize(writer, "NULL"); } else { ((WSEntity)obj).WriteJson(writer, serializer, schema, outFields, printedTypes, Request, CFunc, DBContext); } } #endregion #region PRINT Collection else if (obj.IsCollectionOf <WSEntity>()) { IList list = obj as IList; Type eType = list.GetEntityType(); writer.WriteStartArray(); foreach (WSEntity entity in list) { if (entity != null) { if (entity is WSDynamicEntity) { WSDynamicEntity dEntity = (WSDynamicEntity)entity; if (dEntity.Match(Request, DBContext, CFunc, schema)) { entity.WriteJson(writer, serializer, schema, outFields, printedTypes, Request, CFunc, DBContext); } } else { entity.WriteJson(writer, serializer, schema, outFields, printedTypes, Request, CFunc, DBContext); } } } writer.WriteEndArray(); } #endregion } #endregion } status.AddNote("done", WSConstants.ACCESS_LEVEL.READ); } } catch (Exception e) { status.CODE = WSStatus.ERROR.CODE; status.AddNote("Error(line" + e.LineNumber() + "- " + e.Message + ")"); CFunc.RegError(GetType(), e, ref status); } return(status); }
private bool Validate(object obj, WSParam xParam, JsonWriter writer, JsonSerializer serializer, WSSchema schema, WSSource xSource, WSParamList outFields, ref WSStatus status, WSRequest Request, MetaFunctions CFunc) { try { if (this is WSDynamicEntity && schema == null) { status.CODE = WSStatus.ERROR.CODE; status.AddNote("No schema defined", WSConstants.ACCESS_LEVEL.READ); } else if (xParam == null) { status.CODE = WSStatus.ERROR.CODE; status.AddNote("Undefined parameters are not allowed", WSConstants.ACCESS_LEVEL.READ); } else { bool IsOwner = false;//TODO@ANDVO:2015-09-11 : ADD IsOwner validation check int paramAccessLevel = xParam.READ_ACCESS_MODE.ACCESS_LEVEL; paramAccessLevel = (xSource != null && xSource.AccessLevel > paramAccessLevel) ? xSource.AccessLevel : paramAccessLevel; if (Request.Security.AuthToken.User.role < paramAccessLevel && !IsOwner) { #region ACCESS DENIED HANDLING if (xSource != null && xSource.ShowMessageInaccessible) { string accessNote = "Access denied"; status.CODE = WSStatus.ATTANTION.CODE; status.AddNote(accessNote, WSConstants.ACCESS_LEVEL.READ); WritePropName(writer, xParam.NAME); serializer.Serialize(writer, accessNote); } #endregion } else { if (!WSParamList.IsEmpty(outFields) && !outFields.Any(a => a.Match(schema.Name))) { } else { if (obj == null && !xParam.SkipEmpty) { obj = string.Empty; status.CODE = WSStatus.ATTANTION.CODE; status.AddNote("Can not write NULL to [" + xParam.DISPLAY_NAME + "]. Value set to empty string.", WSConstants.ACCESS_LEVEL.READ); } if (obj != null) { status.AddNote("done", WSConstants.ACCESS_LEVEL.READ); return(true); } } } } } catch (Exception e) { status.CODE = WSStatus.ERROR.CODE; status.AddNote("Error(line" + e.LineNumber() + "- " + e.Message + ")"); CFunc.RegError(GetType(), e, ref status); } return(false); }
private WSStatus WriteJMembers(IEnumerable <MemberInfo> members, JsonWriter writer, JsonSerializer serializer, WSEntitySchema eSchema, WSSource xSource, WSParamList outFields, List <Type> printedTypes, WSRequest Request, MetaFunctions CFunc, WSDataContext DBContext) { WSStatus status = WSStatus.NONE_Copy(); try { Type eType = GetType(); object obj = null; WSParam param = null; if (this is WSDynamicEntity) { foreach (WSSchema fieldSchema in eSchema.Fields /*.Items*/) { if (fieldSchema is WSFieldSchema) { param = ((WSFieldSchema)fieldSchema).param; } else if (fieldSchema is WSEntityBaseSchema) { param = GetParam(xSource, fieldSchema.Name); } //else if (childSchema is WSEntitySchema) { param = GetParam(childSchema.Name); } //else if (childSchema is WSEntityListSchema) { param = GetParam(((WSEntityListSchema)childSchema).EntitySchema.Name); } MemberInfo member = param == null ? null : members.FirstOrDefault(p => param.Match(p.Name, null, null, false)); obj = member == null ? null : member is PropertyInfo ? ((PropertyInfo)member).GetValue(this, null) : member is FieldInfo ? ((FieldInfo)member).GetValue(this) : null; if (param != null) { status.childs.Add(WriteJProperty(obj, param, writer, serializer, fieldSchema, xSource, outFields, printedTypes, Request, CFunc, DBContext)); } } } else if (this is WSStaticEntity) { foreach (MemberInfo member in members) { param = GetParam(xSource, member.Name, member.ReflectedType); obj = member is PropertyInfo ? ((PropertyInfo)member).GetValue(this, null) : member is FieldInfo ? ((FieldInfo)member).GetValue(this) : null; if (param != null) { status.childs.Add(WriteJProperty(obj, param, writer, serializer, null, xSource, outFields, printedTypes, Request, CFunc, DBContext)); } } } status.AddNote("done", WSConstants.ACCESS_LEVEL.READ); } catch (Exception e) { status.CODE = WSStatus.ERROR.CODE; status.AddNote("Error(line" + e.LineNumber() + "- " + e.Message + ")"); CFunc.RegError(GetType(), e, ref status); } return(status); }