/// <summary>
        ///     Updates Enterprise Team
        /// </summary>
        /// <param name="team">Enterprise Team</param>
        /// <returns>Updated Team</returns>
        public async Task <EnterpriseTeam> UpdateTeam(EnterpriseTeam team)
        {
            if (string.IsNullOrEmpty(team.Uid))
            {
                return(await CreateTeam(team));
            }

            if (!TryGetTeam(team.Uid, out _))
            {
                throw new EnterpriseException($"Team UID {team.Uid} not found in enterprise");
            }

            var rq = new TeamUpdateCommand
            {
                TeamUid       = team.Uid,
                TeamName      = team.Name,
                RestrictEdit  = team.RestrictEdit,
                RestrictShare = team.RestrictSharing,
                RestrictView  = team.RestrictView,
                NodeId        = team.ParentNodeId
            };
            await Auth.ExecuteAuthCommand(rq);

            await PopulateEnterprise();

            TryGetTeam(team.Uid, out team);
            return(team);
        }
        /// <summary>
        ///     Creates Enterprise Team.
        /// </summary>
        /// <param name="team">Enterprise Team</param>
        /// <returns>Created Team</returns>
        public async Task <EnterpriseTeam> CreateTeam(EnterpriseTeam team)
        {
            var teamKey = CryptoUtils.GenerateEncryptionKey();

            CryptoUtils.GenerateRsaKey(out var privateKey, out var publicKey);
            var encryptedPrivateKey = CryptoUtils.EncryptAesV1(privateKey, teamKey);
            var teamUid             = CryptoUtils.GenerateUid();
            var rq = new TeamAddCommand
            {
                TeamUid          = teamUid,
                TeamName         = team.Name,
                RestrictEdit     = team.RestrictEdit,
                RestrictShare    = team.RestrictSharing,
                RestrictView     = team.RestrictView,
                PublicKey        = publicKey.Base64UrlEncode(),
                PrivateKey       = encryptedPrivateKey.Base64UrlEncode(),
                NodeId           = team.ParentNodeId,
                ManageOnly       = true,
                EncryptedTeamKey = CryptoUtils.EncryptAesV2(teamKey, TreeKey).Base64UrlEncode()
            };
            await Auth.ExecuteAuthCommand(rq);

            await PopulateEnterprise();

            TryGetTeam(teamUid, out team);
            return(team);
        }
Beispiel #3
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);
                        }
                    }
                }
            }
        }
Beispiel #4
0
 /// <summary>
 /// Gets the enterprise team associated with the specified team UID.
 /// </summary>
 /// <param name="teamUid">Team UID</param>
 /// <param name="team">When this method returns <c>true</c>, contains requested enterprise team; otherwise <c>null</c>.</param>
 /// <returns><c>true</c> in the enterprise contains a team with specified UID; otherwise, <c>false</c></returns>
 public bool TryGetTeam(string teamUid, out EnterpriseTeam team)
 {
     return(_teams.TryGetValue(teamUid, out team));
 }