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); }