示例#1
0
        public Account.Dtos.SessionUserDto GetSessionUser(string userId, string device)
        {
            try
            {
                if (string.IsNullOrEmpty(userId))
                {
                    throw new KnownException("用户名称不能为空!");
                }
                using (var db = new BCEnterpriseContext())
                {
                    ClearUserLoginState(db);

                    var user = (from u in db.FrontUsers
                                where u.UserID == userId
                                join ls in db.UserLoginStates on u.UserID equals ls.UserID
                                where ls.Device == device
                                join ep in db.Enterprises on u.EnterpiseID equals ep.EnterpriseID into tempEP
                                join ur in db.UserRoles on u.UserID equals ur.UserID into tempUR
                                select new
                    {
                        u,
                        ls,
                        ep = tempEP.DefaultIfEmpty().FirstOrDefault(),
                        roles = tempUR.DefaultIfEmpty().Where(n => !n.Deleted).Select(n => n.RoleID),
                        functions = (from tur in tempUR.DefaultIfEmpty()
                                     where tur.Deleted == false
                                     join r in db.RFARoles on tur.RoleID equals r.RoleID
                                     where r.Available
                                     join rf in db.RFAAuthorizations on tur.RoleID equals rf.RoleID into tempRF
                                     from trf in tempRF.DefaultIfEmpty()
                                     where !trf.Deleted
                                     select trf.FunctionID)
                    }).FirstOrDefault();

                    if (user == null)
                    {
                        throw new KnownException("用户名不存在或用户当前登录状态丢失请重新登录。");
                    }

                    #region get functionIds
                    //TODO:get it from the cache
                    var functions   = db.RFAFunctions.ToList();
                    var functionIds = new List <string>();

                    Action <string> recursionFunctions = null;
                    recursionFunctions = (functionId) =>
                    {
                        if (functionIds.Contains(functionId))
                        {
                            return;
                        }
                        functionIds.Add(functionId);

                        var tempParent = (from fun in functions
                                          where fun.FunctionID == functionId
                                          join tfun in functions on fun.ParentID equals tfun.FunctionID
                                          select tfun).FirstOrDefault();
                        if (tempParent != null)
                        {
                            recursionFunctions(tempParent.FunctionID);
                        }
                    };
                    user.functions.ToList().ForEach(recursionFunctions);
                    #endregion

                    var sessionUserDto = new Account.Dtos.SessionUserDto
                    {
                        UserID         = user.u.UserID,
                        UserName       = user.u.Name,
                        LastLoginTime  = user.u.LastDate ?? DateTime.Now,
                        LastIP         = user.u.LastIP,
                        Picture        = UriExtensions.GetFullUrl(user.u.Picture),
                        Device         = user.ls.Device,
                        Token          = user.ls.LoginToken,
                        EnterpriseID   = user.u.EnterpiseID,
                        EnterpriseName = user.ep == null ? string.Empty : user.ep.Name,
                        DepartmentID   = user.u.DepartmentID,
                        RoleIDs        = user.roles.ToArray(),
                        FunctionIDs    = functionIds.ToArray()
                    };
                    return(sessionUserDto);
                }
            }
            catch (Exception)
            {
                throw;
            }
        }
示例#2
0
        public Account.Dtos.SessionUserDto GetSessionUser(string userId)
        {
            using (var db = new BCBackContext())
            {
                var user = (from u in db.BackUsers
                            where u.UserID == userId
                            join ur in db.UserRoles on u.UserID equals ur.UserID into tempUR
                            select new
                {
                    u,
                    roles = tempUR.DefaultIfEmpty().Where(n => !n.Deleted).Select(n => n.RoleID),
                    functions = (from tur in tempUR.DefaultIfEmpty()
                                 where tur.Deleted == false
                                 join r in db.RFARoles on tur.RoleID equals r.RoleID
                                 where r.Available
                                 join rf in db.RFAAuthorizations on tur.RoleID equals rf.RoleID into tempRF
                                 from trf in tempRF.DefaultIfEmpty()
                                 where !trf.Deleted
                                 select trf.FunctionID)
                }).FirstOrDefault();

                #region get functionIds
                //TODO:get it from the cache
                var functions   = db.RFAFunctions.ToList();
                var functionIds = new List <string>();

                Action <string> recursionFunctions = null;
                recursionFunctions = (functionId) =>
                {
                    if (functionIds.Contains(functionId))
                    {
                        return;
                    }
                    functionIds.Add(functionId);
                    var tempParent = (from fun in functions
                                      where fun.FunctionID == functionId
                                      join tfun in functions on fun.ParentID equals tfun.FunctionID
                                      select tfun).FirstOrDefault();
                    if (tempParent != null)
                    {
                        recursionFunctions(tempParent.FunctionID);
                    }
                };
                user.functions.ToList().ForEach(recursionFunctions);
                #endregion
                if (null == user)
                {
                    throw new KnownException("不存在该用户");
                }
                var sessionUserDto = new Account.Dtos.SessionUserDto
                {
                    UserID        = user.u.UserID,
                    UserName      = user.u.Name,
                    LastLoginTime = user.u.LastDate ?? DateTime.Now,
                    LastIP        = user.u.LastIP,
                    RoleIDs       = user.roles.ToArray(),
                    FunctionIDs   = functionIds.ToArray()
                };
                return(sessionUserDto);
            }
        }