예제 #1
0
        /// <summary>
        /// Constructs a basic Lua plugin given the PluginDAO.
        /// </summary>
        /// <param name="plugin">Plugin object to construct this object against</param>
        /// <param name="script">Script that the plugin will run (If it does not have the 
        /// <code>LUAPlugin.LuaExtension</code> at the end of the string, it will be appended.</param>
        public LUAPlugin(PluginDAO plugin, String script)
        {
            if (plugin == null || script == null)
            {
                throw new ArgumentNullException();
            }

            this.ScriptFileLoc = LUADefinitions.getLuaScriptLocation(script);

            // See if it's there
            if (!File.Exists(this.ScriptFileLoc))
            {
                throw new ArgumentException("Cannot find file " + this.ScriptFileLoc);
            }

            this.PluginDAO = plugin;

            // Store sandbox code in a variable so we only call it once
            this.SandboxLuaCode = GetSandBoxCode();
        }
        public void UpdatePluginWithSameInfoShouldReturnSamePlugin()
        {
            _controller.CreatePlugin(_plugin1);
            _controller.UpdatePlugin(_plugin1);
            _plugin2 = _controller.RetrievePlugin(_plugin1.Name);

            Assert.AreEqual(_plugin1, _plugin2, "Updating plugin with same value did not return same information.");
        }
예제 #3
0
 /// <summary>
 /// Constructs a basic Lua plugin given the PluginDAO.
 /// Finds the file of the plugin script by:
 /// <code>LUAPlugin.getLuaScriptLocation(plugin.Name)</code>
 /// </summary>
 /// <param name="plugin">Plugin object to construct this object against</param>
 public LUAPlugin(PluginDAO plugin)
     : this(plugin, plugin.Name)
 {
     // Pass to more defined constructor
 }
예제 #4
0
 /// <summary>
 /// Removes a Plugin from the Group's List of enabled Plugins
 /// </summary>
 /// <param name="plugin">Plugin to be removed</param>
 /// <returns>True if the Plugin is removed successfully, false if otherwise</returns>
 public bool RemovePlugin(PluginDAO plugin)
 {
     return EnabledPlugins.Remove(plugin);
 }
        public void Setup()
        {
            _controller = new SqlController();

            _owner = new UserDAO()
            {
                UserName = "******",
                FirstName = "TEST",
                LastName = "USER",
                PhoneNumber = "1111111111",
                PhoneEmail = "*****@*****.**",
                Carrier = PhoneCarrier.Verizon,
                UserLevel = UserLevel.User,
                IsBanned = false,
                IsSuppressed = false
            };
            _controller.CreateUser(_owner, "password");

            _plugin1 = new PluginDAO()
            {
                Name = "TEST1",
                Description = "A test plugin",
                HelpText = "A simple test plugin",
                IsDisabled = false,
                VersionNum = "1",
                Access = PluginAccess.STANDARD,
                OwnerID = (int) _owner.UserID
            };

            _plugin2 = new PluginDAO()
            {
                Name = "TEST2",
                Description = "A test plugin 2",
                HelpText = "A simple test plugin 2",
                IsDisabled = false,
                VersionNum = "1",
                Access = PluginAccess.STANDARD,
                OwnerID = (int)_owner.UserID
            };

            _nullPlugin = new PluginDAO()
            {
                Name = null,
                Description = null,
                HelpText = null,
                IsDisabled = false,
                VersionNum = null,
                Access = PluginAccess.STANDARD,
                OwnerID = (int)_owner.UserID
            };
        }
예제 #6
0
    public void AddPlugin_Click(Object sender, EventArgs e)
    {
        PluginDAO plugin = new PluginDAO()
        {
            Name = Request["pluginNameBox"],
            Description = Request["pluginDescriptionBox"],
            HelpText = Request["helpTextBox"],
            IsDisabled = false,
            VersionNum = Request["versionBox"],
            OwnerID = _currentUser.UserID
        };

        // Do some form validation
        if (plugin.Name == null || plugin.Name.Length > PluginDAO.NameMaxLength || plugin.Name.Length < 4)
        {
            ShowError(string.Format("Plugin name is invalid. Please enter a name between {0} and {1} characters.", 4, PluginDAO.NameMaxLength));
            pluginNameBox.Focus();
            return;
        }
        else if (plugin.Description == null || plugin.Description.Length >= PluginDAO.DescriptionMaxLength)
        {
            ShowError(string.Format("Plugin description is invalid. Please enter a description less than {0} characters long.", PluginDAO.DescriptionMaxLength));
            pluginDescriptionBox.Focus();
            return;
        }
        else if (plugin.HelpText == null || plugin.HelpText.Length >= PluginDAO.HelpTextMaxLength)
        {
            ShowError(string.Format("Plugin help text is invalid. Please enter a help text less than {0} characters long.", PluginDAO.HelpTextMaxLength));
            helpTextBox.Focus();
            return;
        }
        else if (plugin.VersionNum == null || plugin.VersionNum.Length >= PluginDAO.VersionNumberMaxLength)
        {
            ShowError(string.Format("Plugin version is invalid. Please enter a version that is less than {0} characters long.", PluginDAO.VersionNumberMaxLength));
            versionBox.Focus();
            return;
        }
        else
        {
            // All systems go
            IDBController controller = new SqlController();
            try
            {
                // Can we create our plugin?
                if (controller.CreatePlugin(plugin))
                {
                    // Create a blank file
                    string path = LUADefinitions.getLuaScriptLocation(Request["pluginNameBox"]);
                    try
                    {
                        using (File.Create(path)) { }
                    }
                    catch (Exception)
                    {
                        // Clean up
                        controller.DeletePlugin(plugin);
                        ShowError("Error creating plugin.  Please try again later.");
                        return;
                    }

                    // Shoot them to the editor
                    Response.Redirect(string.Format("EditPlugin.aspx?pluginname={0}", HttpUtility.UrlEncode(plugin.Name)));
                }
            }
            catch (EntryAlreadyExistsException)
            {
                // Error
                ShowError("That plugin name already exists.");
                return;
            }
            catch (SqlException ex)
            {
                // Error
                Logger.LogMessage("AddPlugin.aspx: " + ex.Message, LoggerLevel.SEVERE);
                return;
            }
        }
    }
예제 #7
0
 /// <summary>
 /// Adds a Plugin to the Groups List of enabled Plugins
 /// </summary>
 /// <param name="plugin">Plugin to be added</param>
 /// <returns>True if the Plugin is added successfully, false if otherwise</returns>
 public bool AddPlugin(PluginDAO plugin)
 {
     return EnabledPlugins.Add(plugin);
 }
예제 #8
0
        /// <summary>
        /// Deletes an existing plugin that matches the given PluginDAO.
        /// </summary>
        /// <param name="user">The PluginDAO to delete from the database.</param>
        /// <returns>true if the plugin was successfully deleted.</returns>
        /// <exception cref="ArgumentNullException">If the given PluginDAO or PluginDAO.PluginID is null.</exception>
        public bool DeletePlugin(PluginDAO plugin)
        {
            if (null == plugin || null == plugin.PluginID) throw new ArgumentNullException();

            using (SqlConnection conn = new SqlConnection(CONNECTION_STRING))
            using (SqlCommand query = conn.CreateCommand())
            {
                query.CommandText = "sp_deletePlugin";
                query.CommandType = CommandType.StoredProcedure;
                query.Parameters.AddWithValue("@pluginid", plugin.PluginID);

                conn.Open();
                int effectedRows = query.ExecuteNonQuery();

                /* One or more records should have been deleted:
                 * The plugin record itself (1), and any additional groupplugin entries (0 or more)
                 */
                return 0 < effectedRows;
            }
        }
        public void Setup()
        {
            _controller = new SqlController();

            _owner = new UserDAO()
            {
                UserName = "******",
                FirstName = "TEST",
                LastName = "USER",
                PhoneNumber = "1111111111",
                PhoneEmail = "*****@*****.**",
                Carrier = PhoneCarrier.Verizon,
                UserLevel = UserLevel.User,
                IsBanned = false,
                IsSuppressed = false
            };

            _moderator = new UserDAO()
            {
                UserName = "******",
                FirstName = "TEST",
                LastName = "USER",
                PhoneNumber = "1111111112",
                PhoneEmail = "*****@*****.**",
                Carrier = PhoneCarrier.Verizon,
                UserLevel = UserLevel.User,
                IsBanned = false,
                IsSuppressed = false
            };

            _user = new UserDAO()
            {
                UserName = "******",
                FirstName = "TEST",
                LastName = "USER",
                PhoneNumber = "1111111113",
                PhoneEmail = "*****@*****.**",
                Carrier = PhoneCarrier.Verizon,
                UserLevel = UserLevel.User,
                IsBanned = false,
                IsSuppressed = false
            };

            _controller.CreateUser(_owner, "password");
            _controller.CreateUser(_moderator, "password");
            _controller.CreateUser(_user, "password");

            _enabledPlugin = new PluginDAO()
            {
                Name = "EnPlgn",
                Description = "An enabled test plugin",
                IsDisabled = false,
                VersionNum = "1.0.0",
                OwnerID = _user.UserID,
                Access = PluginAccess.STANDARD,
                HelpText = "Help meh, I'm an enabled plugin!"
            };

            _disabledPlugin = new PluginDAO()
            {
                Name = "DsPlgn",
                Description = "A disabled test plugin",
                IsDisabled = true,
                VersionNum = "1.0.0",
                OwnerID = _user.UserID,
                Access = PluginAccess.STANDARD,
                HelpText = "Help meh, I'm a disabled plugin!"
            };

            _controller.CreatePlugin(_enabledPlugin);
            _controller.CreatePlugin(_disabledPlugin);

            _group = new GroupDAO(_owner)
            {
                Name = "Test Group",
                Description = "A test group, for testing",
                GroupTag = "TEST"
            };
        }
예제 #10
0
        /// <summary>
        /// Updates the given plugin's owner with the given user. On completion, updates
        /// the given PluginDAO's owner id with the given user.
        /// </summary>
        /// <param name="group">The group to update the owner of.</param>
        /// <param name="newOwner">The user to set as owner of the given group.</param>
        /// <returns>true if successful.</returns>
        public bool UpdatePluginOwner(PluginDAO plugin, UserDAO newOwner)
        {
            if (null == plugin || null == newOwner) throw new ArgumentNullException("Cannot update null plugin or user");

            using (SqlConnection conn = new SqlConnection(CONNECTION_STRING))
            using (SqlCommand query = conn.CreateCommand())
            {
                query.CommandText = "UPDATE plugins SET owner_id = @userid WHERE id = @pluginid";
                query.Parameters.AddWithValue("@userid", newOwner.UserID);
                query.Parameters.AddWithValue("@pluginid", plugin.PluginID);

                conn.Open();
                int effectedRows = query.ExecuteNonQuery();

                if (1 == effectedRows)
                {
                    // Update the PluginDAO reference with the new UserDAO
                    plugin.OwnerID = newOwner.UserID;

                    return true;
                }

                return false;
            }
        }
예제 #11
0
 private PluginDAO BuildPluginDAO(SqlDataReader reader)
 {
     PluginDAO pluginDAO = new PluginDAO();
     pluginDAO.PluginID = (int)reader["id"];
     pluginDAO.Name = (string)reader["name"];
     pluginDAO.Description = (string)reader["description"];
     pluginDAO.IsDisabled = (bool)reader["disabled"];
     pluginDAO.VersionNum = (string)reader["version_num"];
     pluginDAO.OwnerID = (int)reader["owner_id"];
     pluginDAO.Access = (PluginAccess)reader["plugin_access"];
     pluginDAO.HelpText = (string)reader["help_text"];
     return pluginDAO;
 }
예제 #12
0
        /// <summary>
        /// 
        /// </summary>
        /// <param name="plugin"></param>
        /// <param name="key"></param>
        /// <param name="value"></param>
        public void UpdatePluginKeyValue(PluginDAO plugin, String key, String value, GroupDAO forGroup = null, UserDAO forUser = null)
        {
            if (null == plugin
                || null == key)
                throw new ArgumentNullException();

            using (SqlConnection conn = new SqlConnection(CONNECTION_STRING))
            using (SqlCommand query = conn.CreateCommand())
            {
                StringBuilder queryBuilder = new StringBuilder();
                // Using SQL Server, so this isn't supported
                //queryBuilder.Append("INSERT INTO pluginkeyvalue (plugin_id, key_string, value_object) ");
                //queryBuilder.Append("VALUES ");
                //queryBuilder.Append("(@pluginid, @keystring, @valueobj) ");
                //queryBuilder.Append("ON DUPLICATE KEY UPDATE ");
                //queryBuilder.Append(" value_object = @valueobj ");

                queryBuilder.Append("begin tran \n");
                queryBuilder.Append("if exists (select * from pluginkeyvalue with (updlock,serializable) where key_string = @keystring and plugin_id = @pluginid and group_id = @groupid) \n");
                queryBuilder.Append("   begin \n");
                queryBuilder.Append("   update pluginkeyvalue set value_object = @valueobj \n");
                queryBuilder.Append("   where key_string = @keystring and plugin_id = @pluginid and group_id = @groupid\n");
                queryBuilder.Append("end \n");
                queryBuilder.Append("else \n");
                queryBuilder.Append("begin \n");
                queryBuilder.Append("   insert pluginkeyvalue (plugin_id, user_id, key_string, value_object, group_id) \n");
                queryBuilder.Append("   values (@pluginid, @userid, @keystring, @valueobj, @groupid) \n");
                queryBuilder.Append("end \n");
                queryBuilder.Append("commit tran \n");

                query.CommandText = queryBuilder.ToString();
                query.Parameters.AddWithValue("@pluginid", plugin.PluginID.Value);
                if (forUser == null)
                    query.Parameters.AddWithValue("@userid", DBNull.Value);
                else
                    query.Parameters.AddWithValue("@userid", forUser.UserID.Value);
                if (forGroup == null)
                    query.Parameters.AddWithValue("@groupid", DBNull.Value);
                else
                    query.Parameters.AddWithValue("@groupid", forGroup.GroupID.Value);
                query.Parameters.AddWithValue("@keystring", key);
                query.Parameters.AddWithValue("@valueobj", value);

                conn.Open();
                query.ExecuteNonQuery();
            }
        }
예제 #13
0
        /// <summary>
        /// Creates a new plugin entry in the database with the given PluginDAO. The PluginID of the given
        /// PluginDAO will also be set after calling this method.
        /// </summary>
        /// <param name="user">The PluginDAO to insert into the database.</param>
        /// <returns>true if the plugin was successfully added and the PluginID was set</returns>
        /// <exception cref="ArgumentNullException">If the given plugin is null.</exception>
        /// <exception cref="EntryAlreadyExistsException">If a plugin already exists with the given PluginDAO.Name.</exception>
        public bool CreatePlugin(PluginDAO plugin)
        {
            if (null == plugin) throw new ArgumentNullException();

            if (PluginExists(plugin.Name)) throw new EntryAlreadyExistsException("Plugin with command: " + plugin.Name + " already exists.");

            using (SqlConnection conn = new SqlConnection(CONNECTION_STRING))
            using (SqlCommand query = conn.CreateCommand())
            {
                StringBuilder queryBuilder = new StringBuilder();
                queryBuilder.Append("INSERT INTO plugins (name, description, disabled, version_num, owner_id, created_dt, plugin_access, help_text) ");
                queryBuilder.Append("VALUES ");
                queryBuilder.Append("(@name, @description, @disabled, @version_num, @owner_id, GETDATE(), @plugin_access, @help_text) ");
                queryBuilder.Append("; SELECT SCOPE_IDENTITY()");

                query.CommandText = queryBuilder.ToString();
                query.Parameters.AddWithValue("@name", plugin.Name);
                query.Parameters.AddWithValue("@description", plugin.Description);
                query.Parameters.AddWithValue("@disabled", plugin.IsDisabled ? 1 : 0);
                query.Parameters.AddWithValue("@version_num", plugin.VersionNum);
                query.Parameters.AddWithValue("@owner_id", plugin.OwnerID);
                query.Parameters.AddWithValue("@plugin_access", (int)plugin.Access);
                query.Parameters.AddWithValue("@help_text", plugin.HelpText);

                conn.Open();
                int newID = (int)(decimal)query.ExecuteScalar();

                // The SCOPE_IDENTITY() should return the generated PluginID of the INSERT statement
                if (1 > newID) return false;

                plugin.PluginID = newID;
                return true;
            }
        }
예제 #14
0
        /// <summary>
        /// Updates plugin information in the database. If there is no entry in the database that matches the given
        /// PluginDAO.PluginID no entries will be updated and <code>false</code> will be returned.
        /// </summary>
        /// <param name="user">The PluginDAO to update in the database</param>
        /// <returns>true if the plugin was successfully updated.</returns>
        /// <exception cref="ArgumentNullException">If the given PluginDAO or PluginDAO.PluginID is null.</exception>
        public bool UpdatePlugin(PluginDAO plugin)
        {
            if (null == plugin || null == plugin.PluginID) throw new ArgumentNullException();

            using (SqlConnection conn = new SqlConnection(CONNECTION_STRING))
            using (SqlCommand query = conn.CreateCommand())
            {
                StringBuilder queryBuilder = new StringBuilder();
                queryBuilder.Append("UPDATE plugins SET ");
                queryBuilder.Append("name = @name, ");
                queryBuilder.Append("description = @description, ");
                queryBuilder.Append("disabled = @disabled, ");
                queryBuilder.Append("version_num = @version_num, ");
                queryBuilder.Append("owner_id = @owner_id, ");
                queryBuilder.Append("plugin_access = @plugin_access, ");
                queryBuilder.Append("help_text = @help_text ");
                queryBuilder.Append("WHERE id = @pluginid");

                query.CommandText = queryBuilder.ToString();
                query.Parameters.AddWithValue("@name", plugin.Name);
                query.Parameters.AddWithValue("@description", plugin.Description);
                query.Parameters.AddWithValue("@disabled", plugin.IsDisabled ? 1 : 0);
                query.Parameters.AddWithValue("@version_num", plugin.VersionNum);
                query.Parameters.AddWithValue("@owner_id", plugin.OwnerID);
                query.Parameters.AddWithValue("@plugin_access", (int)plugin.Access);
                query.Parameters.AddWithValue("@help_text", plugin.HelpText);
                query.Parameters.AddWithValue("@pluginid", plugin.PluginID);

                conn.Open();
                int effectedRows = query.ExecuteNonQuery();

                // Only one record should have been updated
                return 1 == effectedRows;
            }
        }
예제 #15
0
        /// <summary>
        /// 
        /// </summary>
        /// <param name="plugin"></param>
        /// <param name="key"></param>
        /// <returns></returns>
        public string RetrievePluginValue(PluginDAO plugin, String key, GroupDAO forGroup = null, UserDAO forUser = null)
        {
            if (null == plugin
                || null == key)
                throw new ArgumentNullException();

            using (SqlConnection conn = new SqlConnection(CONNECTION_STRING))
            using (SqlCommand query = conn.CreateCommand())
            {
                StringBuilder queryBuilder = new StringBuilder();
                queryBuilder.Append("SELECT value_object ");
                queryBuilder.Append("FROM pluginkeyvalue ");
                queryBuilder.Append("WHERE plugin_id = @pluginid ");
                if (forUser == null)
                    queryBuilder.Append(" AND user_id IS NULL ");
                else
                    queryBuilder.Append(" AND user_id = @userid ");

                if (forGroup == null)
                    queryBuilder.Append(" AND group_id IS NULL ");
                else
                    queryBuilder.Append(" AND group_id = @groupid ");

                queryBuilder.Append(" AND key_string = @keystring ");

                query.CommandText = queryBuilder.ToString();
                query.Parameters.AddWithValue("@pluginid", plugin.PluginID.Value);

                if (forUser != null)
                    query.Parameters.AddWithValue("@userid", forUser.UserID.Value);

                if (forGroup != null)
                    query.Parameters.AddWithValue("@groupid", forGroup.GroupID.Value);

                query.Parameters.AddWithValue("@keystring", key);

                conn.Open();
                SqlDataReader reader = query.ExecuteReader();

                // If there are no records returned from the select statement, the DataReader will be empty
                if (reader.Read())
                {
                    return (string)reader["value_object"];
                }
                else
                {
                    return null;
                }
            }
        }
예제 #16
0
    private void GetPagePlugin()
    {
        if (null == Request["pluginname"])
        {
            Response.Redirect(string.Format(@"Index.aspx?error={0}", HttpUtility.UrlEncode(@"An error occurred retrieving the plugin information")));
            return;
        }

        try
        {
            IDBController controller = new SqlController();
            _currentPlugin = controller.RetrievePlugin(Request["pluginname"]);
        }
        catch (ArgumentNullException)
        {
            // Shouldn't happen
        }
        catch (CouldNotFindException)
        {
            Response.Redirect(string.Format(@"Index.aspx?error={0}", HttpUtility.UrlEncode(@"An unknown error occurred loading plugin data. Please try again soon.")));
        }
        catch (SqlException ex)
        {
            Logger.LogMessage("ManagePlugin: " + ex.Message, LoggerLevel.SEVERE);
            Response.Redirect(string.Format(@"Index.aspx?error={0}", HttpUtility.UrlEncode(@"An unknown error occurred loading plugin data. Please try again soon.")));
        }
    }