예제 #1
0
        public int RegisterServer(int serverId, string userId, string serverUserId, string serverPassword, string serverDomain)
        {
            TeamServer teamServerEntity;

            try
            {
                using (TeamServerRepository teamServerRepository = new TeamServerRepository())
                {
                    teamServerEntity = teamServerRepository.GetById(serverId);
                    if (teamServerEntity == null)
                    {
                        throw new Exception("Invalid server id");
                    }
                }
                using (UserInfoRepository userInfoRepository = new UserInfoRepository())
                {
                    UserInfo userInfoEntity = userInfoRepository.Find(x => x.UserId == userId);
                    if (userInfoEntity == null)
                    {
                        throw new Exception("Invalid user id");
                    }
                }
                using (UserServerInfoRepository userServerInfoRepository = new UserServerInfoRepository())
                {
                    UserServerInfo userServerInfoEntity = userServerInfoRepository.Find(
                        x => x.UserId.ToUpper() == userId.ToUpper() && x.TfsId == serverId);
                    if (userServerInfoEntity != null)
                    {
                        throw new Exception(string.Format("Server {0} is already registered to the user {1} .", serverId, userId));
                    }

                    // Dependency Injection of Team Service.
                    NetworkCredential credential = new NetworkCredential(serverUserId, serverPassword, serverDomain);
                    string            hash       = JsonConvert.SerializeObject(credential).Encrypt();
                    _authenticationService.Authenticate(serverId, hash);

                    userServerInfoEntity = new UserServerInfo()
                    {
                        UserId         = userId,
                        TfsId          = serverId,
                        TfsUserId      = serverUserId,
                        CredentialHash = hash
                    };
                    return(userServerInfoRepository.Insert(userServerInfoEntity));
                }
            }
            catch (Exception ex)
            {
                _logger.Error(ex);
                throw;
            }
        }
        public WorkItemDto FindWorkItemFromServer(int taskid, int serverId, int weekId, string userId)
        {
            WorkItemDto workItemDto = null;
            TeamServer  server      = null;

            using (TeamServerRepository teamServerRepository = new TeamServerRepository())
            {
                server = teamServerRepository.FindLocal(x => x.Id == serverId);
                if (server == null)
                {
                    _logger.Error("Server not found with id " + serverId);
                    throw new Exception("Server not found.");
                }
            }
            string         serverUrl  = server.Url;
            UserServerInfo userServer = null;

            using (UserServerInfoRepository userServerInfoRepository = new UserServerInfoRepository())
            {
                userServer = userServerInfoRepository.Find(x => x.TfsId == serverId && x.UserId == userId);
                if (userServer == null)
                {
                    throw new Exception(string.Format("User {0} not registered to server {1}", userId, serverId));
                }
            }
            WorkItem workItem = _teamServerManagementService.GetWorkItemById(taskid, serverUrl, userServer.CredentialHash);

            if (workItem != null)
            {
                workItemDto          = workItem.ToEntity(serverId).ToDto(-1);
                workItemDto.WeekId   = weekId;
                workItemDto.ServerId = serverId;
                workItemDto.TaskId   = taskid;
            }
            return(workItemDto);
        }