Esempio n. 1
0
        protected CFunctionResult _userLogin(string UserIdentity, string Password, Guid TableID)
        {
            Dictionary<string, object> InputParameters = new Dictionary<string, object>();
            InputParameters.Add("IdentityKey", UserIdentity);
            InputParameters.Add("Password", Password);
            InputParameters.Add("TableID", TableID);

            CFunctionResult R = new CFunctionResult()
            {
                FunctionID = "UserLogin",
                InputParameters = InputParameters,
                ResultType = Communication.EnFunctionResultType.ESuccess
            };

            CFunctionResult UserResult = CServerHelper.sCheckUserByPassword(UserIdentity, Password);
            if (UserResult.ResultType != Communication.EnFunctionResultType.ESuccess)
            {
                UserResult.FunctionID = "UserLogout";
                UserResult.InputParameters = InputParameters;
                return UserResult;
            }

            CSystemUser User = (CSystemUser)UserResult.Content;

            Guid TempGUID = TableID;

            COrganizationTable Table = new COrganizationTable(TempGUID, CServerEnvironment.DataContext);
            if (Table.Key == CDBConst.CONST_OBJECT_EMPTY_KEY)
            {
                R = CServerHelper.sCompileFunctionResult("UserLogin", Communication.EnFunctionResultType.EError, InputParameters, CGlobalizationHelper.sGetStringResource("ERROR_TABLE_UNABLE_TO_FIND", CultureInfo.CurrentCulture), null);
                return R;
            }

            var Links = Table.GetExternalLinks(CServerEnvironment.DataContext);
            if (Links.Count == 0 || Links.Count > 1)
            {
                R = CServerHelper.sCompileFunctionResult("UserLogin", Communication.EnFunctionResultType.EError, InputParameters, CGlobalizationHelper.sGetStringResource("ERROR_ORG_UNABLE_TO_FIND", CultureInfo.CurrentCulture), null);
                return R;
            }

            cMenu.Metaobjects.Linq.CMetaobject Organization = Links[0].GetSourceObject(CServerEnvironment.DataContext);
            if (Organization == null)
            {
                R = CServerHelper.sCompileFunctionResult("UserLogin", Communication.EnFunctionResultType.EError, InputParameters, CGlobalizationHelper.sGetStringResource("ERROR_ORG_UNABLE_TO_FIND", CultureInfo.CurrentCulture), null);
                return R;
            }

            var Sessions = CSystemUserSession.sGetSessionsByUser(User.Key, CServerEnvironment.DataContext);
            CSystemUserSession Session = null;

            if (Sessions.Count != 0)            
            {
                var Query = from Ses in Sessions
                            where 
                                Ses.Status == EnSessionStatus.EEnabled &&
                                Ses.Type == EnSessionType.ETablet &&
                                Ses.DeadLine > DateTime.Now
                            select Ses;
                Session = (Query.Count() == 0 ? null : Query.ToList()[0]);

                if (Session == null)
                {
                    Session = new CSystemUserSession()
                    {
                        ID = Guid.NewGuid(),
                        Key = CDatabaseSequence.sGetObjectKey(CServerEnvironment.DatabaseProvider),
                        Status = EnSessionStatus.EEnabled,
                        Type = EnSessionType.ETablet,
                        UserKey = User.Key,
                        Variables = new byte[0],
                        DeadLine = DateTime.Now.AddHours(8)
                    };
                    Session.VariablesDictionary.Add(CSecurityConsts.CONST_SESSION_TABLE_ID, Table.ID);
                    Session.SessionVariablesSave();

                    var RR = Session.SessionInsert(CServerEnvironment.DataContext);
                    if (RR != CErrors.ERR_SUC)
                    {
                        R = CServerHelper.sCompileFunctionResult("UserLogin", Communication.EnFunctionResultType.EError, InputParameters, CGlobalizationHelper.sGetStringResource("ERROR_SESSION_INSERT_UNABLE", CultureInfo.CurrentCulture), null);
                        return R;
                    }
                }
                else
                    Session.SessionVariablesLoad();
            }
            else
            {
                Session = new CSystemUserSession()
                {
                    ID = Guid.NewGuid(),
                    Key = CDatabaseSequence.sGetObjectKey(CServerEnvironment.DatabaseProvider),
                    Status = EnSessionStatus.EEnabled,
                    Type = EnSessionType.ETablet,
                    UserKey = User.Key,
                    Variables = new byte[0],
                    DeadLine = DateTime.Now.AddHours(8)
                };
                Session.VariablesDictionary.Add(CSecurityConsts.CONST_SESSION_TABLE_ID, Table.ID);
                Session.SessionVariablesSave();

                var RR = Session.SessionInsert(CServerEnvironment.DataContext);
                if (RR != CErrors.ERR_SUC)
                {
                    R = CServerHelper.sCompileFunctionResult("UserLogin", Communication.EnFunctionResultType.EError, InputParameters, CGlobalizationHelper.sGetStringResource("ERROR_SESSION_INSERT_UNABLE", CultureInfo.CurrentCulture), null);
                    return R;
                }
            }

            try
            { CServerEnvironment.DataContext.SubmitChanges(); }
            catch (Exception Exception)
            {
                R = CServerHelper.sCompileFunctionResult("UserLogin", Communication.EnFunctionResultType.EError, InputParameters, Exception.Message, null);
                return R;
            }

            var Content = new object[3];
            Content[0] = User;
            Content[1] = Session;
            Content[2] = Organization;

            R.Content = Content;

            return R;
        }
Esempio n. 2
0
        public static CFunctionResult sCheckUser(string UserIdentity, string Passhash, Guid SessionID)
        {
            CFunctionResult R = new CFunctionResult()
            {
                FunctionID = "CheckUser",
                ResultType = EnFunctionResultType.ESuccess
            };

            if (UserIdentity == null || Passhash == null || SessionID == null)
            {
                R = CServerHelper.sCompileFunctionResult("CheckUser", Communication.EnFunctionResultType.EError, null, "ERROR_USER_INPUT_PARAMS_NULL", null);
                return R;
            }
            if (UserIdentity.Trim() == "")
            {
                R = CServerHelper.sCompileFunctionResult("CheckUser", Communication.EnFunctionResultType.EError, null, "ERROR_USER_INPUT_PARAMS_NULL", null);
                return R;
            }

            CSystemUser User = null;

            User = CSystemUser.sGetUserByLogin(UserIdentity.Trim(), CServerEnvironment.DataContext);
            if (User == null)
            {
                User = CSystemUser.sGetUserByEmail(UserIdentity.Trim(), CServerEnvironment.DataContext);
                if (User == null)
                    User = CSystemUser.sGetUserByMobilePhone(UserIdentity.Trim(), CServerEnvironment.DataContext);
            }

            if (User == null)
            {
                R = CServerHelper.sCompileFunctionResult("CheckUser", Communication.EnFunctionResultType.EError, null, "ERROR_OBJECT_UNABLE_TO_FIND", null);
                return R;
            }

            var Verified = (Passhash == User.Passhash);
            if (!Verified)
            {
                R = CServerHelper.sCompileFunctionResult("CheckUser", Communication.EnFunctionResultType.EError, null, "ERROR_USER_BAD_IDENTITY_OR_PASS", null);
                return R;
            }

            CSystemUserSession Session = new CSystemUserSession() { ID = SessionID };
            var RR = Session.SessionGetByID(SessionID, CServerEnvironment.DataContext);
            if (RR != CErrors.ERR_SUC)
            {
                R = CServerHelper.sCompileFunctionResult("CheckUser", Communication.EnFunctionResultType.EError, null, "ERROR_SESSION_NULL", null);
                return R;
            }

            if (Session.UserKey != User.Key)
            {
                R = CServerHelper.sCompileFunctionResult("CheckUser", Communication.EnFunctionResultType.EError, null, "ERROR_SESSION_KEYS_NOT_EQUALS", null);
                return R;
            }

            if (Session.DeadLine <= DateTime.Now)
            {
                R = CServerHelper.sCompileFunctionResult("CheckUser", Communication.EnFunctionResultType.EError, null, "ERROR_SESSION_DEADLINE_EXPIRES", null);
                return R;
            }

            if (Session.Status != EnSessionStatus.EEnabled)
            {
                R = CServerHelper.sCompileFunctionResult("CheckUser", Communication.EnFunctionResultType.EError, null, "ERROR_SESSION_NOT_ENABLED", null);
                return R;
            }

            var Content = new object[2];
            Content[0] = User;
            Content[1] = Session;

            R.Content = Content;

            return R;
        }