Exemple #1
0
        public async Task <IActionResult> Post([FromBody] LoginRequire require)
        {
            if (string.IsNullOrEmpty(require.User) || string.IsNullOrEmpty(require.Password))
            {
                return(Ok(new { Succeed = false, Error = "User account or password is null" }));
            }

#if FUTURE
            //TODO:以下逻辑合并至ServerMessageDispatcher.ProcessLoginRequire

            //根据账号索引查询
            var q = new IndexGet(appbox.Consts.SYS_EMPLOEE_MODEL_ID, appbox.Consts.EMPLOEE_UI_ACCOUNT_ID);
            q.Where(new KeyPredicate(appbox.Consts.EMPLOEE_ACCOUNT_ID, KeyPredicateType.Equal, require.User));
            var res = await q.ToIndexRowAsync();

            if (res.IsEmpty)
            {
                return(Ok(new { Succeed = false, Error = "User account not exists" }));
            }
            Guid   emploeeID = res.TargetEntityId;
            byte[] passData  = res.ValueTuple.GetBytes(appbox.Consts.EMPLOEE_PASSWORD_ID);
            res.Dispose();
            //验证密码
            if (!RuntimeContext.PasswordHasher.VerifyHashedPassword(passData, require.Password))
            {
                return(Ok(new { Succeed = false, Error = "Password not match" }));
            }

            //TODO:****暂全表扫描获取Emploee对应的OrgUnits,待用Include EntitySet实现
            var q1 = new TableScan(appbox.Consts.SYS_ORGUNIT_MODEL_ID);
            q1.Filter(q1.GetGuid(appbox.Consts.ORGUNIT_BASEID_ID) == emploeeID);
            var ous = await q1.ToListAsync();

            if (ous == null || ous.Count == 0)
            {
                return(Ok(new { Succeed = false, Error = "User must mapped to OrgUnit" }));
            }

            TreeNodePath path = await EntityStore.LoadTreeNodePathAsync(
                appbox.Consts.SYS_ORGUNIT_MODEL_ID, ous[0].Id, appbox.Consts.ORGUNIT_PARENT_ID, appbox.Consts.ORGUNIT_NAME_ID);
#else
            //查找账号并验证密码
            var q = new SqlQuery(appbox.Consts.SYS_EMPLOEE_MODEL_ID);
            q.Where(q.T["Account"] == require.User);
            var emp = await q.ToSingleAsync();

            if (emp == null)
            {
                return(Ok(new { Succeed = false, Error = "User account not exists" }));
            }
            Guid emploeeID = emp.GetGuid(StoreInitiator.PK_Member_Id);

            byte[] passData = emp.GetBytes(appbox.Consts.EMPLOEE_PASSWORD_ID);
            if (passData == null)
            {
                return(Ok(new { Succeed = false, Error = "User password not exists" }));
            }

            if (!RuntimeContext.PasswordHasher.VerifyHashedPassword(passData, require.Password))
            {
                return(Ok(new { Succeed = false, Error = "Password not match" }));
            }
            //查找对应的OrgUnits
            var q1 = new SqlQuery(appbox.Consts.SYS_ORGUNIT_MODEL_ID);
            q1.Where(q1.T["BaseId"] == emploeeID);
            var ous = await q1.ToListAsync();

            if (ous == null || ous.Count == 0)
            {
                return(Ok(new { Succeed = false, Error = "User must mapped to OrgUnit" }));
            }

            var q2 = new SqlQuery(appbox.Consts.SYS_ORGUNIT_MODEL_ID);
            q2.Where(q2.T["Id"] == ous[0].GetGuid(StoreInitiator.PK_Member_Id));
            TreeNodePath path = await q2.ToTreeNodePathAsync(q2.T["Parent"], q2.T["Name"]);
#endif

            object returnUserInfo = new { ous[0].Id, Name = path[0].Text, Account = require.User };

            //注册会话
            var id      = (ulong)StringHelper.GetHashCode(require.User); //TODO:***** 暂简单hash
            var session = new WebSession(id, path, emploeeID, null /*TODO:tag暂null*/);
            HttpContext.Session.SaveWebSession(session);

            //返回登录成功
            Log.Debug($"用户[{session.GetFullName()}]登录.");
            return(Ok(new { Succeed = true, UserInfo = returnUserInfo }));
        }