示例#1
0
        protected static bool ResetAdmin()
        {
            bool bRet = true;

            try
            {
                SBConfigStor.Directory dTmp = new SBConfigStor.Directory();
                if (dTmp == null)
                {
                    Console.Error.WriteLine("ERROR: Couldn't create Directory object!");
                }
                else
                {
                    bool   bRes    = true;
                    string sUserid = "";

                    bool bFound = SBConfigStor.Directory.GetUseridByUsername(g_sAdminUsername, out sUserid);
                    if (!bFound)
                    {
                        // Add admin user before continuing.

                        Console.Error.WriteLine("WARNING: Admin user not found, adding now.");

                        SBConfigStor.Users.User uTmp = new SBConfigStor.Users.User();
                        uTmp.Username = g_sAdminUsername;

                        SBConfigStor.Directory.AddUser(uTmp);

                        bFound = SBConfigStor.Directory.GetUseridByUsername(g_sAdminUsername, out sUserid);
                        if (!bFound)
                        {
                            bRes = false;
                            Console.Error.WriteLine("ERROR: Couldn't find admin user that was just added to the DB!");
                        }
                    }

                    // Set the password
                    if (bRes)
                    {
                        bRes = dTmp.SetPassByUserid(sUserid, g_sAdminUsername.ToUpper(), g_sAdminUsername.ToLower());
                        if (!bRes)
                        {
                            Console.Error.WriteLine("ERROR:  Couldn't set the password.");
                        }
                        else
                        {
                            Console.WriteLine("Admin password was reset.");
                        }
                    }
                }
            }
            catch (Exception exc)
            {
                bRet = false;
                Console.Error.WriteLine("ERROR: Caught exception: {0}", exc.ToString());
            }

            return(bRet);
        }         // ResetAdmin
示例#2
0
        protected static bool AddUser(string i_sUsername, string i_sPassword, string i_sDefaultWebpage)
        {
            bool          bRet  = true;
            StringBuilder sbSql = null;

            try
            {
                SBConfigStor.Directory dTmp = new SBConfigStor.Directory();
                if (dTmp == null)
                {
                    Console.Error.WriteLine("ERROR: Couldn't create Directory object!");
                }
                else
                {
                    bool   bRes    = true;
                    string sUserid = "";


                    // Create the user

                    SBConfigStor.Users.User uTmp = new SBConfigStor.Users.User();
                    uTmp.Username = i_sUsername;

                    SBConfigStor.Directory.AddUser(uTmp);

                    bool bFound = SBConfigStor.Directory.GetUseridByUsername(i_sUsername, out sUserid);
                    if (!bFound)
                    {
                        bRes = false;
                        Console.Error.WriteLine("ERROR: Couldn't find user that was just added to the DB!");
                    }
                    else
                    {
                        Console.WriteLine("  User created.");

                        // Set the password
                        if (bRes)
                        {
                            bRes = dTmp.SetPassByUserid(sUserid, i_sUsername, i_sPassword);
                            if (!bRes)
                            {
                                Console.Error.WriteLine("ERROR:  Couldn't set the password.");
                            }
                            else
                            {
                                Console.WriteLine("  User's password set.");
                            }
                        }

                        if ((i_sDefaultWebpage == null) || (i_sDefaultWebpage.Length <= 0))
                        {
                            // Not an error, just don't do anything.
                        }
                        else
                        {
                            // Set the default webpage
                            sbSql = new StringBuilder();
                            sbSql.AppendFormat("INSERT INTO tblUserParams (uUserId, iParamType, sParamName, sParamValue) VALUES ('{0}', '{1}', '{2}', '{3}');", sUserid, (int)(UserPrefs.eParamType.DefaultWebpage), UserPrefs.eParamType.DefaultWebpage.ToString(), i_sDefaultWebpage);

                            bRes = RunSqlCmd(sbSql.ToString());
                            if (bRes)
                            {
                                Console.WriteLine("  User's default webpage set.");
                            }
                        }
                    }
                }
            }
            catch (Exception exc)
            {
                bRet = false;
                Console.Error.WriteLine("ERROR: Caught exception: {0}", exc.ToString());
            }

            return(bRet);
        }         // AddUser
        /// <summary>
        /// This supports CSV upload with the required columns being LName, FName, and Ext.
        /// Optional columns currently supported are E-mail, Username, Domain, Mobile Number and Pager Number.
        /// Future support for any additional columns will have to be added here.
        /// </summary>
        public bool Parse(byte[] i_abBytes, out Users o_Users)
        {
            bool bRet = true;

            o_Users = new Users();

            try
            {
                int        ii, iArrSize, iPrevStart, iNumStrs;
                ArrayList  lIndexes, lFields;
                string     sLine = "", sLName = "", sFName = "", sExt = "";
                string     sEmail = "", sUser = "", sDomain = "";
                string     sMobileNumber = "";
                string     sPagerNumber  = "";
                Users.User tmpUser       = null;
                Encoding   oEncoding     = null;
                byte[]     abLine        = null;

                // Break byte array into individual strings
                iArrSize   = i_abBytes.Length;
                iPrevStart = 0;
                iNumStrs   = 0;
                lIndexes   = new ArrayList();

                for (ii = 0; ii < iArrSize; ii++)
                {
                    if ((i_abBytes[ii] == m_acSeparators[(int)eSeparators.CR]) || (i_abBytes[ii] == m_acSeparators[(int)eSeparators.LF]))
                    {
                        i_abBytes[ii] = 0;
                        if ((ii + 1) < iArrSize)
                        {
                            ii++;
                            while ((ii < iArrSize) && ((i_abBytes[ii] == m_acSeparators[(int)eSeparators.CR]) || (i_abBytes[ii] == m_acSeparators[(int)eSeparators.LF])))
                            {
                                //i_abBytes[ii] = 0;	//string.TrimEnd doesn't seem to work on '\0'.
                                ii++;
                            }

                            lIndexes.Add(iPrevStart);
                            iPrevStart = ii;
                            iNumStrs++;
                        }
                    }
                }
                lIndexes.Add(iArrSize - 1);

                // Parse each string
                lFields = new ArrayList();
                for (ii = 0; ii < iNumStrs; ii++)
                {
                    sLine         = "";
                    sLName        = "";
                    sFName        = "";
                    sExt          = "";
                    sEmail        = "";
                    sUser         = "";
                    sDomain       = "";
                    sMobileNumber = "";
                    sPagerNumber  = "";

                    // Convert from Windows file encoding to UTF8
                    // FIX - Always safe to assume the file is Windows encoded???  Will probably cause LumenVox to fail if they upload a CSV file created on Linux.
                    oEncoding = Encoding.GetEncoding("windows-1252");
                    abLine    = Encoding.Convert(oEncoding, Encoding.UTF8, i_abBytes, (int)lIndexes[ii], ((int)lIndexes[ii + 1] - (int)lIndexes[ii]));
                    sLine     = Encoding.UTF8.GetString(abLine);

                    sLine = sLine.TrimEnd(m_acSeparators);

                    if (ii > 0)                         // Skip the first line, assumed to be a header.
                    {
                        bool bRes = GetFields(sLine, lFields);

                        // Insert valid entries into DB
                        sLName = (string)lFields[0];                                    // FIX - Assumes [lname, fname, ext]
                        sFName = (string)lFields[1];
                        sExt   = (string)lFields[2];
                        if (lFields.Count > 3)
                        {
                            sEmail = (string)lFields[3];
                        }
                        if (lFields.Count > 4)
                        {
                            sUser = (string)lFields[4];
                        }
                        if (lFields.Count > 5)
                        {
                            sDomain = (string)lFields[5];
                        }
                        if (lFields.Count > 6)
                        {
                            sMobileNumber = (string)lFields[6];
                        }
                        if (lFields.Count > 7)
                        {
                            sPagerNumber = (string)lFields[7];
                        }

                        //if( (sLName.Length == 0) && (sFName.Length == 0 && (sExt.Length == 0) )	// Ignore line, it was empty
                        //if(sExt.Length == 0)	// Ignore line, it was malformed
                        if ((sLName.Length == 0) && (sFName.Length == 0) && (sExt.Length == 0) && (sEmail.Length == 0) && (sUser.Length == 0) && (sDomain.Length == 0))
                        {                               // NOP
                        }
                        else
                        {
                            tmpUser              = new SBConfigStor.Users.User();
                            tmpUser.LName        = sLName;
                            tmpUser.FName        = sFName;
                            tmpUser.Ext          = sExt;
                            tmpUser.Email        = sEmail;
                            tmpUser.Username     = sUser;
                            tmpUser.Domain       = sDomain;
                            tmpUser.MobileNumber = sMobileNumber;
                            tmpUser.PagerNumber  = sPagerNumber;

                            o_Users.Add(tmpUser);
                        }
                    }             // if
                }                 // for
            }
            catch (Exception exc)
            {
                bRet = false;
                Console.Error.WriteLine(DateTime.Now.ToString() + "SBConfigStore.CsvImportParser.Parse exception: " + exc.ToString());
            }

            return(bRet);
        }