public GroupDAO(UserDAO Owner) { this.Owner = Owner; this.Users = new HashSet<UserDAO>(); this.Moderators = new HashSet<UserDAO>(); this.EnabledPlugins = new HashSet<PluginDAO>(); }
public void Setup() { this.stubbedController = MockRepository.GenerateStub<SqlController>(); this._user1 = new UserDAO() { UserName = "******", FirstName = "TEST", LastName = "USER", PhoneNumber = "1111111111", PhoneEmail = "*****@*****.**", Carrier = PhoneCarrier.Verizon, UserLevel = UserLevel.User, IsBanned = false, IsSuppressed = false }; this._user2 = new UserDAO() { UserName = "******", FirstName = "TEST", LastName = "USER", PhoneNumber = "1111111112", PhoneEmail = "*****@*****.**", Carrier = PhoneCarrier.Verizon, UserLevel = UserLevel.User, IsBanned = false, IsSuppressed = false }; this._user3 = new UserDAO() { UserName = "******", FirstName = "TEST", LastName = "USER", PhoneNumber = "1111111113", PhoneEmail = "*****@*****.**", Carrier = PhoneCarrier.Verizon, UserLevel = UserLevel.User, IsBanned = false, IsSuppressed = false }; this._group = new GroupDAO(this._user1) { Description = "Test description", GroupID = 1, GroupTag = "TEST", Moderators = new HashSet<UserDAO>(), Name = "TEST GROUP", EnabledPlugins = new HashSet<PluginDAO>(), Users = new HashSet<UserDAO>() }; this._group.Users.Add(this._user2); this.stubbedController.Stub(x => x.RetrieveGroup(this._group.GroupTag)).Return(this._group); this.stubbedController.Stub(x => x.RetrieveUserByPhoneEmail(this._user1.PhoneEmail)).Return(this._user1); this.stubbedController.Stub(x => x.RetrieveUserByPhoneEmail(this._user2.PhoneEmail)).Return(this._user2); }
/// <summary> /// Deletes an existing user that matches the given UserDAO. /// </summary> /// <param name="user">The UserDAO to delete from the database.</param> /// <param name="checkIsOwnerBeforeRemoving">If true, checks to see if the user is an owner of a group or plugin.</param> /// <returns>true if the user was successfully deleted. Returns false if the user cannot be deleted due to them owning a Plugin or Group.</returns> /// <exception cref="ArgumentNullException">If the given UserDAO or UserDAO.UserID is null.</exception> public bool DeleteUser(UserDAO user, bool checkIsOwnerBeforeRemoving) { if (null == user || null == user.UserID) throw new ArgumentNullException(); if (checkIsOwnerBeforeRemoving && CheckIfOwnerOfGroupOrPlugin(user)) return false; using (SqlConnection conn = new SqlConnection(CONNECTION_STRING)) using (SqlCommand query = conn.CreateCommand()) { query.CommandText = "sp_deleteUser"; query.CommandType = CommandType.StoredProcedure; query.Parameters.AddWithValue("@userid", user.UserID); conn.Open(); int effectedRows = query.ExecuteNonQuery(); /* One or more records should have been deleted: * The user record itself (1), and any additional groupmember entries (0 or more) */ return effectedRows > 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; } } }
/// <summary> /// Deletes an existing user that matches the given UserDAO. The user should not be admin of a group, or have plugins /// </summary> /// <param name="user">The UserDAO to delete from the database.</param> /// <returns>true if the user was successfully deleted. Returns false if the user cannot be deleted due to them owning a Plugin or Group.</returns> /// <exception cref="ArgumentNullException">If the given UserDAO or UserDAO.UserID is null.</exception> public bool DeleteUser(UserDAO user) { // We should always check before deleting return DeleteUser(user, true); }
/// <summary> /// Adds a Moderator to the Group /// </summary> /// <param name="user">User to be added as a Moderator</param> /// <returns>True if User is added as a Moderator successfully, false if otherwise</returns> public bool AddModerator(UserDAO user) { return Moderators.Add(user); }
/// <summary> /// Removes a User as a Moderator for the Group /// </summary> /// <param name="user">User to be removed from Moderators</param> /// <returns>True if User is removed successfully, false if otherwise</returns> public bool RemoveModerator(UserDAO user) { return Moderators.Remove(user); }
/// <summary> /// Bans a user to prevent them from sending or receiving texts. /// </summary> /// <param name="pluginID">The user to ban.</param> /// <returns>true if successful.</returns> /// <exception cref="ArgumentNullException">If the given user is null.</exception> public bool BanUser(UserDAO user) { return ToggleUserBanned(user, true); }
public void Setup() { _user1 = new UserDAO() { UserName = "******", FirstName = "TEST", LastName = "USER", PhoneNumber = "1111111111", PhoneEmail = "*****@*****.**", Carrier = PhoneCarrier.Verizon, UserLevel = UserLevel.User, IsBanned = false, IsSuppressed = false }; _user2 = new UserDAO() { UserName = "******", FirstName = "TEST", LastName = "USER", PhoneNumber = "1111111112", PhoneEmail = "*****@*****.**", Carrier = PhoneCarrier.Verizon, UserLevel = UserLevel.User, IsBanned = false, IsSuppressed = false }; _nullUser = new UserDAO() { UserName = null, FirstName = null, LastName = null, PhoneNumber = null, PhoneEmail = null, Carrier = PhoneCarrier.Verizon, UserLevel = UserLevel.User, IsBanned = false, IsSuppressed = false }; _controller = new SqlController(); }
/// <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(); } }
/// <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; } }
/// <summary> /// Unsuppresses a user so they can continue to receive texts. /// </summary> /// <param name="pluginID">The user to unsuppress.</param> /// <returns>true if successful.</returns> /// <exception cref="ArgumentNullException">If the given user is null.</exception> public bool UnsuppressUser(UserDAO user) { return ToggleUserSuppression(user, false); }
/// <summary> /// Updates the given group's owner with the given user. On completion, updates /// the given GroupDAO's owner object 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 UpdateGroupOwner(GroupDAO group, UserDAO newOwner) { if (null == group || null == newOwner) throw new ArgumentNullException("Cannot update null group or user"); using (SqlConnection conn = new SqlConnection(CONNECTION_STRING)) using (SqlCommand query = conn.CreateCommand()) { query.CommandText = "UPDATE groups SET owner_id = @userid WHERE id = @groupid"; query.Parameters.AddWithValue("@userid", newOwner.UserID); query.Parameters.AddWithValue("@groupid", group.GroupID); conn.Open(); int effectedRows = query.ExecuteNonQuery(); if (1 == effectedRows) { // Update the GroupDAO reference with the new UserDAO group = new GroupDAO(newOwner) { GroupID = group.GroupID, Name = group.Name, Description = group.Description, GroupTag = group.GroupTag, Moderators = group.Moderators, Users = group.Users, EnabledPlugins = group.EnabledPlugins }; return true; } return false; } }
/// <summary> /// Unbans a user so they can continue to send and receive texts. /// </summary> /// <param name="pluginID">The user to unban.</param> /// <returns>true if successful.</returns> /// <exception cref="ArgumentNullException">If the given user is null.</exception> public bool UnbanUser(UserDAO user) { return ToggleUserBanned(user, false); }
/// <summary> /// Suppresses a user to prevent them from receiving texts. /// </summary> /// <param name="pluginID">The user to suppress.</param> /// <returns>true if successful.</returns> /// <exception cref="ArgumentNullException">If the given user is null.</exception> public bool SuppressUser(UserDAO user) { return ToggleUserSuppression(user, true); }
/// <summary> /// Sets the verification code for the given user /// </summary> /// <param name="verificationCode">The 6-character verification code.</param> /// <param name="user">The user to set the verification code for.</param> /// <returns>true if successful.</returns> /// <exception cref="ArgumentNullException">If the verification is null or empty, or the userDAO or UserDAO.UserID are null.</exception> public bool SetVerificationCodeForUser(string verificationCode, UserDAO user) { if ((null != verificationCode && verificationCode.Trim().Equals(string.Empty)) || null == user || null == user.UserID) throw new ArgumentNullException(); using (SqlConnection conn = new SqlConnection(CONNECTION_STRING)) using (SqlCommand query = conn.CreateCommand()) { StringBuilder queryBuilder = new StringBuilder(); queryBuilder.Append("UPDATE users "); queryBuilder.Append("SET verification_code = @verificationCode "); queryBuilder.Append("WHERE id = @userid "); query.CommandText = queryBuilder.ToString(); if (null == verificationCode) query.Parameters.AddWithValue("@verificationCode", DBNull.Value); else query.Parameters.AddWithValue("@verificationCode", verificationCode); query.Parameters.AddWithValue("@userid", user.UserID); conn.Open(); int effectedRows = query.ExecuteNonQuery(); // Only one record should have been affected return 1 == effectedRows; } }
/// <summary> /// Gets the current verification code for the given user. Useful for determining if a user /// has been verified in the system completely. /// </summary> /// <param name="user">The user to check in the database.</param> /// <returns>the database string with either "-1" (new user), a 6-character verification code (registering), or NULL (verified).</returns> public string GetCurrentVerificationValueForUser(UserDAO user) { if (null == user || null == user.UserID) throw new ArgumentNullException(); using (SqlConnection conn = new SqlConnection(CONNECTION_STRING)) using (SqlCommand query = conn.CreateCommand()) { StringBuilder queryBuilder = new StringBuilder(); queryBuilder.Append("SELECT verification_code "); queryBuilder.Append("FROM users "); queryBuilder.Append("WHERE id = @userid "); query.CommandText = queryBuilder.ToString(); query.Parameters.AddWithValue("@userid", user.UserID); conn.Open(); SqlDataReader reader = query.ExecuteReader(); // If there are no records returned from the select statement, the DataReader will be empty if (reader.Read()) { if (DBNull.Value.Equals(reader["verification_code"])) return null; else return reader["verification_code"] as string; } else throw new CouldNotFindException("Could not find user with userid: " + user.UserID); } }
/// <summary> /// Updates user information in the database. If there is no entry in the database that matches the given /// UserDAO.UserID and UserDAO.PhoneEmail, no entries will be updated and <code>false</code> will be returned. /// </summary> /// <param name="user">The UserDAO to update in the database</param> /// <returns>true if the user was successfully updated.</returns> /// <exception cref="ArgumentNullException">If the given UserDAO or UserDAO.UserID is null.</exception> public bool UpdateUser(UserDAO user) { if (null == user || null == user.UserID) throw new ArgumentNullException(); using (SqlConnection conn = new SqlConnection(CONNECTION_STRING)) using (SqlCommand query = conn.CreateCommand()) { StringBuilder queryBuilder = new StringBuilder(); queryBuilder.Append("UPDATE users SET "); queryBuilder.Append("username = @username, "); queryBuilder.Append("first_name = @first_name, "); queryBuilder.Append("last_name = @last_name, "); queryBuilder.Append("phone = @phone, "); queryBuilder.Append("email_phone = @email_phone, "); queryBuilder.Append("carrier = @carrier, "); queryBuilder.Append("user_level = @user_level, "); queryBuilder.Append("banned = @banned, "); queryBuilder.Append("suppressed = @suppressed "); queryBuilder.Append("WHERE id = @userid "); query.CommandText = queryBuilder.ToString(); query.Parameters.AddWithValue("@username", user.UserName); query.Parameters.AddWithValue("@first_name", user.FirstName); query.Parameters.AddWithValue("@last_name", user.LastName); query.Parameters.AddWithValue("@phone", user.PhoneNumber); query.Parameters.AddWithValue("@email_phone", user.PhoneEmail); query.Parameters.AddWithValue("@carrier", (int) user.Carrier); query.Parameters.AddWithValue("@user_level", (int) user.UserLevel); query.Parameters.AddWithValue("@banned", user.IsBanned ? 1 : 0); query.Parameters.AddWithValue("@suppressed", user.IsSuppressed ? 1 : 0); query.Parameters.AddWithValue("@userid", user.UserID); conn.Open(); int effectedRows = query.ExecuteNonQuery(); // Only one record should have been updated return 1 == effectedRows; } }
/// <summary> /// Gets a list of plugins that are owned by the given user. /// </summary> /// <param name="user">The user to retrieve a list of owned plugins for.</param> /// <returns>A list containing the plugins owned by the user. If the user does not own any plugins, the list will return empty.</returns> public List<PluginDAO> GetPluginsOwnedByUser(UserDAO user) { if (null == user || null == user.UserID) throw new ArgumentNullException("Cannot get plugins for null owner"); using (SqlConnection conn = new SqlConnection(CONNECTION_STRING)) using (SqlCommand query = conn.CreateCommand()) { StringBuilder queryBuilder = new StringBuilder(); queryBuilder.Append("SELECT id, name, description, disabled, version_num, owner_id, plugin_access, help_text "); queryBuilder.Append("FROM plugins "); queryBuilder.Append("WHERE owner_id = @ownerid "); query.CommandText = queryBuilder.ToString(); query.Parameters.AddWithValue("@ownerid", user.UserID); conn.Open(); SqlDataReader reader = query.ExecuteReader(); List<PluginDAO> plugins = new List<PluginDAO>(); while (reader.Read()) { plugins.Add(BuildPluginDAO(reader)); } return plugins; } }
private UserDAO BuildUserDAO(SqlDataReader reader) { UserDAO userDAO = new UserDAO(); userDAO.UserID = (int)reader["id"]; userDAO.UserName = (string)reader["username"]; userDAO.FirstName = (string)reader["first_name"]; userDAO.LastName = (string)reader["last_name"]; userDAO.PhoneNumber = (string)reader["phone"]; userDAO.PhoneEmail = (string)reader["email_phone"]; userDAO.Carrier = (PhoneCarrier)(int)reader["carrier"]; userDAO.UserLevel = (UserLevel)reader["user_level"]; userDAO.IsBanned = (bool)reader["banned"]; userDAO.IsSuppressed = (bool)reader["suppressed"]; return userDAO; }
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" }; }
/// <summary> /// Checks if the given user is the owner of a group or plugin. /// </summary> /// <param name="user">The user to check in the database.</param> /// <returns>true if the user is an owner.</returns> private bool CheckIfOwnerOfGroupOrPlugin(UserDAO user) { using (SqlConnection conn = new SqlConnection(CONNECTION_STRING)) using (SqlCommand query = conn.CreateCommand()) { query.CommandText = "SELECT COUNT(*) FROM groups, plugins WHERE owner_id = @userid"; query.Parameters.AddWithValue("@userid", user.UserID); conn.Open(); int effectedRows = query.ExecuteNonQuery(); /* If 1 or more records exist, then the user owns a group or plugin */ return effectedRows > 0; } }
public void UpdateUserWithSameInfoShouldReturnSameUser() { _controller.CreateUser(_user1, "password"); _controller.UpdateUser(_user1); _user2 = _controller.RetrieveUserByUserName(_user1.UserName); Assert.AreEqual(_user1.UserName, _user2.UserName, "UserNames do not match"); Assert.AreEqual(_user1.FirstName, _user2.FirstName, "FirstNames do not match."); Assert.AreEqual(_user1.LastName, _user2.LastName, "LastNames do not match."); Assert.AreEqual(_user1.PhoneNumber, _user2.PhoneNumber, "PhoneNumbers do not match."); Assert.AreEqual(_user1.PhoneEmail, _user2.PhoneEmail, "PhoneEmails do not match."); Assert.AreEqual(_user1.UserLevel, _user2.UserLevel, "UserLevels do not match."); }
/// <summary> /// Removes a User from the Group /// </summary> /// <param name="user">User to be removed</param> /// <returns>True if User was removed successfully, false if otherwise</returns> public bool RemoveUserFromGroup(UserDAO user) { return Users.Remove(user); }
/// <summary> /// Adds a User to the Group /// </summary> /// <param name="user">User to be added</param> /// <returns>True if User was added successfully, False if otherwise</returns> public bool AddUserToGroup(UserDAO user) { return Users.Add(user); }
public void TearDown() { this._user2 = null; this._user1 = null; this._group = null; this.stubbedController = null; }
/// <summary> /// Creates a new user entry in the database with the given UserDAO. /// The UserID of the given UserDAO will also be set after calling this method. /// </summary> /// <param name="user">The UserDAO to insert into the database.</param> /// <param name="password">The password for the user.</param> /// <returns>true if the user was successfully added and the UserID was set</returns> /// <exception cref="ArgumentNullException">If the given UserDAO or password is null.</exception> /// <exception cref="SqlException">If there is an error querying the database.</exception> public bool CreateUser(UserDAO user, string password) { if (null == user || string.IsNullOrEmpty(password)) throw new ArgumentNullException(); if (UserExists(user.UserName, user.PhoneEmail)) throw new EntryAlreadyExistsException("User with username: "******" or phone email: " + user.PhoneEmail + " already exists."); using (SqlConnection conn = new SqlConnection(CONNECTION_STRING)) using (SqlCommand query = conn.CreateCommand()) { StringBuilder queryBuilder = new StringBuilder(); queryBuilder.Append("INSERT INTO users (username, password, salt, first_name, last_name, phone, email_phone, carrier, user_level, banned, suppressed, created_dt) "); queryBuilder.Append("VALUES "); queryBuilder.Append("(@username, CONVERT(VARBINARY, HASHBYTES('SHA1', @password)), @salt, @first_name, @last_name, @phone, @email_phone, @carrier, @user_level, @banned, @suppressed, GETDATE()) "); queryBuilder.Append("; SELECT SCOPE_IDENTITY()"); String salt = GenerateSalt(128); query.CommandText = queryBuilder.ToString(); query.Parameters.AddWithValue("@username", user.UserName); query.Parameters.AddWithValue("@password", password + salt); query.Parameters.AddWithValue("@salt", salt); query.Parameters.AddWithValue("@first_name", user.FirstName); query.Parameters.AddWithValue("@last_name", user.LastName); query.Parameters.AddWithValue("@phone", user.PhoneNumber); query.Parameters.AddWithValue("@email_phone", user.PhoneEmail); query.Parameters.AddWithValue("@carrier", (int)user.Carrier); query.Parameters.AddWithValue("@user_level", (int)user.UserLevel); query.Parameters.AddWithValue("@banned", user.IsBanned ? 1 : 0); query.Parameters.AddWithValue("@suppressed", user.IsSuppressed ? 1 : 0); conn.Open(); int newID = (int)(decimal) query.ExecuteScalar(); // The SCOPE_IDENTITY() should return the generated UserID of the INSERT statement if (1 > newID) return false; user.UserID = newID; return true; } }
private void TryRegisterSystem() { // Try and register all of our plugins // But, first it has to be associated to a user // So lets try and make one first UserDAO systemUser = new UserDAO() { UserLevel = UserLevel.SuperUser, FirstName = "System", LastName = "Account", UserName = "******", IsBanned = false, IsSuppressed = false, PhoneNumber = "1112223333", PhoneEmail = "*****@*****.**", Carrier = PhoneCarrier.Verizon }; try { this.idbController.CreateUser(systemUser, VerificationGenerator.GenerateString(120)); this.idbController.SetVerificationCodeForUser(null, systemUser); } catch { // Already defined systemUser = this.idbController.RetrieveUserByUserName(systemUser.UserName); } // Now lets add our plugins foreach (KeyValuePair<string, IPlugin> pluginPair in defaultPlugins) { try { PluginDAO plugin = pluginPair.Value.PluginDAO; plugin.Name = pluginPair.Key; plugin.OwnerID = systemUser.UserID; this.idbController.CreatePlugin(plugin); } catch { } } }
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 }; }
private bool ToggleUserSuppression(UserDAO user, bool value) { if (null == user) throw new ArgumentNullException("Cannot update suppression for null user."); using (SqlConnection conn = new SqlConnection(CONNECTION_STRING)) using (SqlCommand query = conn.CreateCommand()) { StringBuilder queryBuilder = new StringBuilder(); queryBuilder.Append("UPDATE users "); queryBuilder.Append("SET suppressed = @value "); queryBuilder.Append("WHERE id = @user_id "); query.CommandText = queryBuilder.ToString(); query.Parameters.AddWithValue("@value", value); query.Parameters.AddWithValue("@user_id", user.UserID); conn.Open(); int effectedRows = query.ExecuteNonQuery(); return 1 == effectedRows; } }