예제 #1
0
        /// <summary>
        /// Retrieves Enterprise node structure.
        /// </summary>
        /// <returns>Awaitable task.</returns>
        public async Task PopulateEnterprise(bool nodesOnly = false)
        {
            var includes = new HashSet <string>();

            includes.Add("nodes");
            if (!nodesOnly)
            {
                includes.Add("users");
                includes.Add("teams");
                includes.Add("team_users");
            }

            var rs = await GetEnterpriseData(includes.ToArray());

            var ids = new HashSet <long>();

            if (includes.Contains("nodes"))
            {
                ids.Clear();
                ids.UnionWith(_nodes.Keys);
                foreach (var n in rs.Nodes)
                {
                    if (_nodes.TryGetValue(n.NodeId, out var node))
                    {
                        ids.Remove(n.NodeId);
                        node.Subnodes.Clear();
                    }
                    else
                    {
                        node = new EnterpriseNode {
                            Id = n.NodeId
                        };
                        _nodes.TryAdd(n.NodeId, node);
                    }

                    EnterpriseUtils.DecryptEncryptedData(n, TreeKey, node);

                    if (n.ParentId.HasValue && n.ParentId.Value > 0)
                    {
                        node.ParentNodeId = n.ParentId.Value;
                    }
                    else
                    {
                        RootNode             = node;
                        RootNode.DisplayName = rs.EnterpriseName;
                        node.ParentNodeId    = 0;
                    }
                }
                foreach (var id in ids)
                {
                    _nodes.TryRemove(id, out _);
                }

                foreach (var node in _nodes.Values)
                {
                    if (node.ParentNodeId <= 0)
                    {
                        continue;
                    }
                    if (_nodes.TryGetValue(node.ParentNodeId, out var parent))
                    {
                        parent.Subnodes.Add(node.Id);
                    }
                }
            }

            if (includes.Contains("users"))
            {
                if (rs.Users != null)
                {
                    ids.Clear();
                    ids.UnionWith(_users.Keys);
                    foreach (var u in rs.Users)
                    {
                        if (_users.TryGetValue(u.EnterpriseUserId, out var user))
                        {
                            ids.Remove(u.EnterpriseUserId);
                            user.Teams.Clear();
                        }
                        else
                        {
                            user = new EnterpriseUser
                            {
                                Id = u.EnterpriseUserId
                            };
                            _users.TryAdd(u.EnterpriseUserId, user);
                        }

                        user.ParentNodeId = u.NodeId;
                        user.Email        = u.Username;
                        EnterpriseUtils.DecryptEncryptedData(u, TreeKey, user);

                        if (u.Status == "active")
                        {
                            switch (u.Lock)
                            {
                            case 0:
                                user.UserStatus = UserStatus.Active;
                                break;

                            case 1:
                                user.UserStatus = UserStatus.Locked;
                                break;

                            case 2:
                                user.UserStatus = UserStatus.Disabled;
                                break;

                            default:
                                user.UserStatus = UserStatus.Active;
                                break;
                            }

                            if (u.AccountShareExpiration.HasValue && u.AccountShareExpiration.Value > 0)
                            {
                                var now = DateTimeOffset.UtcNow.ToUnixTimeMilliseconds();
                                if (now > (long)u.AccountShareExpiration.Value)
                                {
                                    user.UserStatus = UserStatus.Blocked;
                                }
                            }
                        }
                        else
                        {
                            user.UserStatus = UserStatus.Inactive;
                        }
                    }

                    foreach (var id in ids)
                    {
                        _users.TryRemove(id, out _);
                    }

                    _userNames.Clear();
                    foreach (var u in rs.Users)
                    {
                        _userNames.TryAdd(u.Username, u.EnterpriseUserId);
                    }
                }
            }

            if (includes.Contains("teams"))
            {
                if (rs.Teams != null)
                {
                    var uids = new HashSet <string>();
                    uids.UnionWith(_teams.Keys);
                    foreach (var t in rs.Teams)
                    {
                        if (_teams.TryGetValue(t.TeamUid, out var team))
                        {
                            uids.Remove(t.TeamUid);
                            team.Users.Clear();
                        }
                        else
                        {
                            team = new EnterpriseTeam
                            {
                                Uid = t.TeamUid
                            };
                            _teams.TryAdd(t.TeamUid, team);
                        }

                        team.Name = t.Name;

                        team.ParentNodeId    = t.NodeId;
                        team.RestrictEdit    = t.RestrictEdit;
                        team.RestrictSharing = t.RestrictSharing;
                        team.RestrictView    = t.RestrictView;
                        if (!string.IsNullOrEmpty(t.EncryptedTeamKey))
                        {
                            try
                            {
                                team.TeamKey = CryptoUtils.DecryptAesV2(t.EncryptedTeamKey.Base64UrlDecode(), TreeKey);
                            }
                            catch (Exception e)
                            {
                                Debug.WriteLine(e.Message);
                            }
                        }
                    }

                    foreach (var uid in uids)
                    {
                        _teams.TryRemove(uid, out _);
                    }
                }
            }

            if (includes.Contains("team_users"))
            {
                if (rs.TeamUsers != null)
                {
                    foreach (var tu in rs.TeamUsers)
                    {
                        if (_users.TryGetValue(tu.EnterpriseUserId, out var user) && _teams.TryGetValue(tu.TeamUid, out var team))
                        {
                            team.Users.Add(user.Id);
                            user.Teams.Add(team.Uid);
                        }
                    }
                }
            }
        }
예제 #2
0
 /// <summary>
 /// Gets the enterprise node associated with the specified ID.
 /// </summary>
 /// <param name="nodeId">Node Enterprise ID</param>
 /// <param name="node">When this method returns <c>true</c>, contains requested enterprise node; otherwise <c>null</c>.</param>
 /// <returns><c>true</c> in the enterprise contains a node with specified ID; otherwise, <c>false</c></returns>
 public bool TryGetNode(long nodeId, out EnterpriseNode node)
 {
     return(_nodes.TryGetValue(nodeId, out node));
 }