Exemplo n.º 1
0
 private TWikiUser[] GetAllGroupMembers(string groupName)
 {
     TWikiGroup tWikiGroup = null;
     if (!_convertedGroups.TryGetValue(groupName, out tWikiGroup))
     {
         return new TWikiUser[0];
     }
     if (tWikiGroup == null)
     {
         return new TWikiUser[0];
     }
     Dictionary<string, TWikiUser> members = new Dictionary<string, TWikiUser>(StringComparer.InvariantCultureIgnoreCase);
     Dictionary<string, TWikiGroup> visitedGroups = new Dictionary<string, TWikiGroup>(StringComparer.InvariantCultureIgnoreCase);
     IntGetGroupMemebers(tWikiGroup, members, visitedGroups);
     TWikiUser[] result = new TWikiUser[members.Count];
     members.Values.CopyTo(result, 0);
     return result;
 }
Exemplo n.º 2
0
        private void ConvertUsers(string htpasswdFilePath)
        {
            WriteLineToConsole("Converting users...");
            string[] userLines = File.ReadAllLines(htpasswdFilePath);

            Dictionary<string, string> dekiUsers = new Dictionary<string, string>();

            DreamMessage usersResponse = _dekiPlug.At("users").With("limit", int.MaxValue).GetAsync().Wait();

            if (usersResponse.Status != DreamStatus.Ok)
            {
                WriteErrorResponse(usersResponse, "Error while reciving users from Deki. Users not converted.");
                return;
            }

            XDoc dekiUsersDoc = usersResponse.AsDocument();
            foreach (XDoc userDoc in dekiUsersDoc["//user"])
            {
                string dekiUserName = userDoc["nick"].AsText;
                dekiUsers[dekiUserName.ToLower()] = null;
            }

            foreach (string userLine in userLines)
            {
                string user = userLine.Trim();
                if (string.IsNullOrEmpty(user))
                {
                    continue;
                }
                int colonIndex = user.IndexOf(':');
                if (colonIndex <= 0)
                {
                    continue;
                }
                string userName = user.Substring(0, colonIndex);
                if (_convertedUsers.ContainsKey(userName))
                {
                    continue;
                }
                string eMail = null;
                int lastColonIndex = user.LastIndexOf(':');
                if ((lastColonIndex > 0) && (lastColonIndex > colonIndex) && (lastColonIndex + 1 < user.Length))
                {
                    eMail = user.Substring(lastColonIndex + 1, user.Length - lastColonIndex - 1);
                }

                string newUserName = userName;
                int userNum = 0;
                while (dekiUsers.ContainsKey(newUserName.ToLower()))
                {
                    userNum++;
                    newUserName = userName + userNum.ToString();
                }

                if (newUserName != userName)
                {
                    WriteLineToConsole("TWiki user \"" + userName + "\" converted as \"" + newUserName + "\" becouse of existing same user in Deki");
                }

                XDoc usersDoc = new XDoc("user")
                    .Elem("username", newUserName);
                if (!string.IsNullOrEmpty(eMail))
                {
                    usersDoc.Elem("email", eMail);
                }

                string newPassword = Guid.NewGuid().ToString();

                DreamMessage res = _dekiPlug.At("users").With("accountpassword", newPassword).PostAsync(usersDoc).Wait();
                WriteErrorResponse(res, "Error converting user \"" + userName + "\"");

                XDoc resDoc = res.AsDocument();
                int dekiId = resDoc["@id"].AsInt.Value;

                _convertedUsers[userName] = new TWikiUser(userName, newUserName, dekiId);
            }

            WriteLineToConsole("Users converted!!!");
            WriteLineToConsole();
        }
Exemplo n.º 3
0
        private void LoadConvertedUsersAndGroups()
        {
            if (!File.Exists(ConvertedUsersFileName))
            {
                return;
            }

            WriteLineToConsole("Loading converted users from " + ConvertedUsersFileName);

            XDoc convertedUsers = XDocFactory.LoadFrom(ConvertedUsersFileName, MimeType.XML);

            if ((convertedUsers == null) || (convertedUsers.IsEmpty))
            {
                return;
            }
            foreach (XDoc userDoc in convertedUsers["//" + ConvertedUserTagName])
            {
                string tWikiUserName = userDoc["@" + TWikiUserNameAttribute].AsText;
                if (tWikiUserName == null)
                {
                    continue;
                }
                string dekiUserName = userDoc["@" + DekiUserNameAttribute].AsText;
                if (dekiUserName == null)
                {
                    continue;
                }

                DreamMessage dekiUserMessage = _dekiPlug.At("users", "=" + XUri.DoubleEncodeSegment(dekiUserName)).GetAsync().Wait();
                if (dekiUserMessage.Status != DreamStatus.Ok)
                {
                    if (dekiUserMessage.Status == DreamStatus.NotFound)
                    {
                        WriteLineToConsole("User \"" + dekiUserName+"\" specified in \""+ ConvertedUsersFileName+ "\" but not found in Deki. New user created." );
                    }
                    else
                    {
                        WriteErrorResponse(dekiUserMessage, "Error reciving user data from Deki.");
                    }
                    continue;
                }
                XDoc dekiUserDoc = dekiUserMessage.AsDocument();
                int dekiUserId = dekiUserDoc["@id"].AsInt.Value;

                _convertedUsers[tWikiUserName] = new TWikiUser(tWikiUserName, dekiUserName, dekiUserId);
            }
            foreach (XDoc groupDoc in convertedUsers["//" + ConvertedGroupTagName])
            {
                string tWikiGroupName = groupDoc["@" + TWikiGroupNameAttribute].AsText;
                if (tWikiGroupName == null)
                {
                    continue;
                }
                string dekiGroupName = groupDoc["@" + DekiGroupNameAttribute].AsText;
                if (dekiGroupName == null)
                {
                    continue;
                }

                _convertedGroups[tWikiGroupName] = new TWikiGroup(dekiGroupName, false, tWikiGroupName);
            }
            WriteLineToConsole("Users loaded!!!");
            WriteLineToConsole();
        }