public async Task <ActionResult> Post([FromBody] Database.User user) { if (!ModelState.IsValid) { return(BadRequest()); } try { var result = await _user.Post(user); if (result != null) { return(Created(new Uri(Url.Link("GetId", new { id = result.Id })), result)); } else { return(BadRequest()); } } catch (ArgumentException ex) { return(StatusCode((int)HttpStatusCode.InternalServerError, ex.Message)); } }
public async Task <ActionResult> Put([FromBody] Database.User user) { if (!ModelState.IsValid) { return(BadRequest()); } try { var result = await _user.Put(user); if (result != null) { return(Ok(result)); } else { return(BadRequest()); } } catch (ArgumentException ex) { return(StatusCode((int)HttpStatusCode.InternalServerError, ex.Message)); } }
/// <summary> /// 修改用户密码 /// </summary> /// <param name="OldPassword">旧密码</param> /// <param name="NewPassword">新密码</param> /// <returns></returns> public bool ChangePassword(User user, string OldPassword, string NewPassword) { try { NewPassword = UtilEncrypt.MD5Encoding(NewPassword); if (user.Password.Equals(NewPassword)) { return false; }else{ user.Password = NewPassword; db.SaveChanges(); } } catch (Exception ex) { Console.WriteLine(ex.Message); return false; } return true; }
/// <summary> /// 添加用户 /// </summary> /// <param name="username">用户名称</param> /// <param name="password">密码</param> /// <param name="Email">邮箱地址</param> /// <param name="Cellphone">联系电话</param> /// <returns></returns> public int CreatUser(string UserName, string Password, string Email, string Cellphone) { //1.确认用户名和密码是否为空,如果为空,返回 -1 if (string.IsNullOrEmpty(UserName) || string.IsNullOrEmpty(Password)) return 0; //UNDONE:2.用户名是否无效【如含有特殊符号】 //if (!IsNormalCharacter(UserName)) //{ // return 0; //} //3.电子邮件地址是否已存在,如果存在,返回 7 if (IsEmailExist(Email, null)) { return 7; } //4.确认用户名称是否已经使用过,如果已经使用过,返回 6 if (IsUsernameExist(UserName,null)) { return 6; }else{ User user = new User(); user.Username = UserName; //5.密码需要进行加密,采用md5不可逆编码 Password = UtilEncrypt.MD5Encoding(Password); user.Password = Password; user.Email = Email; user.Cellphone = Cellphone; user.LoginTimes = 1; user.CommitTime = DateTime.Now; user.UpdateTime = DateTime.Now; db.User.Add(user); db.SaveChanges(); } return 0; }
public ArrayList RetrieveAllUsers( ) { ArrayList users = new ArrayList( ); string userCommand = "SELECT * FROM " + UserTable; SqlCommand cmd = new SqlCommand( userCommand, SqlDataConnection ); SqlDataReader dataReader = cmd.ExecuteReader( ); User retrievedUser = null; if ( dataReader.HasRows ) { while( dataReader.Read( ) ) { retrievedUser = new User( ); retrievedUser.ID = dataReader["UserID"].ToString( ); retrievedUser.ExternID = dataReader["ExternalID"].ToString( ); retrievedUser.Name = dataReader["UserName"].ToString( ); retrievedUser.PermissionSettings = new Permissions( ); retrievedUser.PermissionSettings.CreateTests = (dataReader["CanCreateTests"].ToString( ) == "True"); retrievedUser.PermissionSettings.ModifyTests = (dataReader["CanModifyTests"].ToString( ) == "True"); retrievedUser.PermissionSettings.DeleteTests = (dataReader["CanDeleteTests"].ToString( ) == "True"); retrievedUser.PermissionSettings.CreateAreas = (dataReader["CanCreateAreas"].ToString( ) == "True"); retrievedUser.PermissionSettings.ModifyAreas = (dataReader["CanModifyAreas"].ToString( ) == "True"); retrievedUser.PermissionSettings.DeleteAreas = (dataReader["CanDeleteAreas"].ToString( ) == "True"); users.Add( retrievedUser ); } dataReader.Close( ); } return users; }
/// <summary> /// 用于向 Users EntitySet 添加新对象的方法,已弃用。请考虑改用关联的 ObjectSet<T> 属性的 .Add 方法。 /// </summary> public void AddToUsers(User user) { base.AddObject("Users", user); }
/// <summary> /// 创建新的 User 对象。 /// </summary> /// <param name="id">ID 属性的初始值。</param> /// <param name="userName">UserName 属性的初始值。</param> /// <param name="password">Password 属性的初始值。</param> /// <param name="personalID">PersonalID 属性的初始值。</param> /// <param name="status">Status 属性的初始值。</param> /// <param name="role">Role 属性的初始值。</param> public static User CreateUser(global::System.Int32 id, global::System.String userName, global::System.String password, global::System.String personalID, global::System.String status, global::System.String role) { User user = new User(); user.ID = id; user.UserName = userName; user.Password = password; user.PersonalID = personalID; user.Status = status; user.Role = role; return user; }
public void VerifyVariousTransactionColumnManipulations() { var fromObject = new User { Id = Guid.NewGuid(), Name = "John Doe" }; DatabaseSession.Instance.Connector.CreateTableWithColumns(fromObject); var transaction = DatabaseSession.Instance.CreateTransaction(); DatabaseSession.Instance.Connector.CreatePrimaryKeyConstraint(fromObject, transaction); DatabaseSession.Instance.CommitTransaction(transaction); DatabaseSession.Instance.Connector.DeleteTable(fromObject); }
public void VerifyThatUniquenessConstraintWorks() { var fromObject = new User { Id = Guid.NewGuid(), Name = "John Doe" }; fromObject.Usergroup_Id = 3; DatabaseSession.Instance.Connector.CreateTableWithColumns(fromObject); Assert.Throws<InvalidDataException>( () => DatabaseSession.Instance.Connector.DeleteUniquenessConstraint(new[] { fromObject.GetType().GetProperty("Usergroup_Id") }, fromObject)); Assert.Throws<ArgumentException>( () => DatabaseSession.Instance.Connector.CreateUniquenessConstraint(null, fromObject)); Assert.Throws<ArgumentException>( () => DatabaseSession.Instance.Connector.DeleteUniquenessConstraint(null, fromObject)); DatabaseSession.Instance.Connector.CreateUniquenessConstraint(new []{ fromObject.GetType().GetProperty("Usergroup_Id") }, fromObject ); Assert.Throws<InvalidDataException>( () => DatabaseSession.Instance.Connector.CreateUniquenessConstraint(new[] { fromObject.GetType().GetProperty("Usergroup_Id") }, fromObject)); Assert.DoesNotThrow(() => DatabaseSession.Instance.Connector.DeleteUniquenessConstraint(new[] { fromObject.GetType().GetProperty("Usergroup_Id") }, fromObject)); Assert.DoesNotThrow(() => fromObject.Save()); DatabaseSession.Instance.Connector.DeleteTable(new User()); }
private static void PrintUserInformation( User user ) { Console.WriteLine( "User Information:" ); Console.WriteLine( "\tUser Name: \t\t" + user.Name ); Console.WriteLine( "\tExternal Account: \t" + user.ExternID ); Console.WriteLine( "\tInternal GUID: \t\t" + user.ID ); Console.WriteLine( "\tCanCreateTests: \t" + user.PermissionSettings.CreateTests ); Console.WriteLine( "\tCanDeleteTests: \t" + user.PermissionSettings.DeleteTests ); Console.WriteLine( "\tCanModifyTests: \t" + user.PermissionSettings.ModifyTests ); Console.WriteLine( "\tCanCreateAreas: \t" + user.PermissionSettings.CreateAreas ); Console.WriteLine( "\tCanDeleteAreas: \t" + user.PermissionSettings.DeleteAreas ); Console.WriteLine( "\tCanModifyAreas: \t" + user.PermissionSettings.ModifyAreas ); }
public void VerifyThatOverridesAndForeignKeyConstraintsWork() { var fromObject = new User { Id = Guid.NewGuid(), Name = "John Doe" }; var toObject = new Usergroup { Iid = 1, Name = "Admin" }; fromObject.Usergroup_Id = toObject.Iid; DatabaseSession.Instance.Connector.CreateTableWithColumns(toObject); DatabaseSession.Instance.Connector.CreateTableWithColumns(fromObject); Assert.Throws<InvalidDataException>(() => DatabaseSession.Instance.Connector.CreateForeignKeyConstraint(fromObject.GetType().GetProperty("Usergroup_Id"), fromObject, toObject.GetType().GetProperty(toObject.PrimaryKey), toObject)); DatabaseSession.Instance.Connector.CreatePrimaryKeyConstraint(toObject); Assert.Throws<InvalidDataException>( () => DatabaseSession.Instance.Connector.DeleteForeignKeyConstraint( fromObject.GetType().GetProperty("Usergroup_Id"), fromObject, toObject.GetType().GetProperty(toObject.PrimaryKey), toObject)); Assert.DoesNotThrow(() => DatabaseSession.Instance.Connector.CreateForeignKeyConstraint(fromObject.GetType().GetProperty("Usergroup_Id"), fromObject, toObject.GetType().GetProperty(toObject.PrimaryKey), toObject)); Assert.Throws<InvalidDataException>(() => fromObject.Save()); toObject.Save(); Assert.DoesNotThrow(() => fromObject.Save()); Assert.Throws<InvalidDataException>(() => toObject.Delete()); DatabaseSession.Instance.Connector.DeleteForeignKeyConstraint(fromObject.GetType().GetProperty("Usergroup_Id"), fromObject, toObject.GetType().GetProperty(toObject.PrimaryKey), toObject); Assert.DoesNotThrow(() => toObject.Delete()); DatabaseSession.Instance.Connector.DeleteTable(new User()); DatabaseSession.Instance.Connector.DeleteTable(new Usergroup()); }
public void VerifyThatNotNullConstraintWorks() { var fromObject = new User { Id = Guid.NewGuid(), Name = "John Doe" }; fromObject.Usergroup_Id = 3; DatabaseSession.Instance.Connector.CreateTableWithColumns(fromObject); DatabaseSession.Instance.Connector.CreatePrimaryKeyConstraint(fromObject); Assert.Throws<ArgumentNullException>( () => DatabaseSession.Instance.Connector.CreateNotNullConstraint(null, fromObject)); Assert.Throws<ArgumentNullException>( () => DatabaseSession.Instance.Connector.DeleteNotNullConstraint(null, fromObject)); fromObject.Name = null; fromObject.Save(); Assert.Throws<InvalidDataException>( () => DatabaseSession.Instance.Connector.CreateNotNullConstraint(fromObject.GetType().GetProperty("Name"), fromObject)); fromObject.Name = "somename"; fromObject.Save(); DatabaseSession.Instance.Connector.CreateNotNullConstraint(fromObject.GetType().GetProperty("Name"), fromObject); fromObject.Name = null; Assert.Throws<InvalidDataException>(() => fromObject.Save()); Assert.DoesNotThrow(() => DatabaseSession.Instance.Connector.DeleteNotNullConstraint(fromObject.GetType().GetProperty("Name"), fromObject)); fromObject.Name = null; Assert.DoesNotThrow(() => fromObject.Save()); DatabaseSession.Instance.Connector.DeleteTable(new User()); }
public void VerifyThatDefaultWorks() { var fromObject = new User { Id = Guid.NewGuid(), Name = null }; fromObject.Usergroup_Id = 3; var testname = "James"; Assert.Throws<ArgumentNullException>(()=> DatabaseSession.Instance.Connector.SetDefault(null, testname, fromObject)); Assert.Throws<ArgumentNullException>(() => DatabaseSession.Instance.Connector.DeleteDefault(null, fromObject)); DatabaseSession.Instance.Connector.CreateTableWithColumns(fromObject); DatabaseSession.Instance.Connector.SetDefault(fromObject.GetType().GetProperty("Name"), testname , fromObject); // save some names fromObject.Save(ignoreNull: true); var object2 = new User { Id = Guid.NewGuid(), Name = null }; object2.Save(ignoreNull: true); Assert.AreEqual(2, User.All().Where(x => x.Name == testname).ToList().Count); DatabaseSession.Instance.Connector.DeleteDefault(fromObject.GetType().GetProperty("Name"), fromObject); var object3 = new User { Id = Guid.NewGuid(), Name = null }; object3.Save(ignoreNull: true); Assert.AreEqual(2, User.All().Where(x => x.Name == testname).ToList().Count); Assert.AreEqual(3, User.All().ToList().Count); Assert.AreEqual(2, User.Subset(2,1, fromObject.GetType().GetProperty("Name")).ToList().Count); DatabaseSession.Instance.Connector.DeleteTable(new User()); }
/// <summary> /// Retrieve a user by its ID... /// </summary> /// <param name="ID"></param> /// <returns></returns> public User RetrieveUser( string ID ) { string userCommand = "SELECT * FROM " + UserTable + " WHERE UserID='" + ID + "'"; SqlCommand cmd = new SqlCommand( userCommand, SqlDataConnection ); SqlDataReader dataReader = cmd.ExecuteReader( ); User retrievedUser = new User( ); retrievedUser.ID = ""; if ( dataReader.HasRows ) { dataReader.Read( ); retrievedUser.ID = dataReader["UserID"].ToString( ); retrievedUser.ExternID = dataReader["ExternalID"].ToString( ); retrievedUser.Name = dataReader["UserName"].ToString( ); retrievedUser.PermissionSettings = new Permissions( ); retrievedUser.PermissionSettings.CreateTests = (dataReader["CanCreateTests"].ToString( ) == "True"); retrievedUser.PermissionSettings.ModifyTests = (dataReader["CanModifyTests"].ToString( ) == "True"); retrievedUser.PermissionSettings.DeleteTests = (dataReader["CanDeleteTests"].ToString( ) == "True"); retrievedUser.PermissionSettings.CreateAreas = (dataReader["CanCreateAreas"].ToString( ) == "True"); retrievedUser.PermissionSettings.ModifyAreas = (dataReader["CanModifyAreas"].ToString( ) == "True"); retrievedUser.PermissionSettings.DeleteAreas = (dataReader["CanDeleteAreas"].ToString( ) == "True"); } dataReader.Close( ); return retrievedUser; }
public void VerifyThatTransactionSafeCreateTableWorks() { var fromObject = new User { Id = Guid.NewGuid(), Name = "John Doe" }; var transaction = DatabaseSession.Instance.CreateTransaction(); var prop = fromObject.GetType().GetProperties().First(); DatabaseSession.Instance.Connector.CreateTable(fromObject, transaction); DatabaseSession.Instance.Connector.CreateColumn(prop, fromObject, transaction); DatabaseSession.Instance.CommitTransaction(transaction); DatabaseSession.Instance.Connector.DeleteTable(fromObject); var allTypes = new AllTypeThing(); DatabaseSession.Instance.Connector.CreateTable(allTypes); transaction = DatabaseSession.Instance.CreateTransaction(); DatabaseSession.Instance.Connector.CreateTable(fromObject, transaction); Assert.Throws<InvalidDataException>(() => allTypes.Save(transaction: transaction)); DatabaseSession.Instance.CommitTransaction(transaction); // TODO: Unclear why but transactions stopped working all of a sudden DatabaseSession.Instance.Connector.DeleteTable(allTypes); DatabaseSession.Instance.Connector.DeleteTable(fromObject); Assert.IsFalse(DatabaseSession.Instance.Connector.CheckTableExists(fromObject)); }
/// <summary> /// Adds a user by taking in an instance of a user object. /// </summary> /// <param name="UserName"></param> /// <param name="ExternalID"></param> /// <param name="PermissionSettings"></param> public void AddUser( User user ) { string userCommand = "INSERT " + UserTable + " (UserID,ExternalID,UserName,CanCreateTests,CanDeleteTests,CanModifyTests,CanCreateAreas,CanDeleteAreas,CanModifyAreas) " + "VALUES ('" + user.ID + "','" + user.ExternID + "','" + user.Name + "'," + BoolToBitString( user.PermissionSettings.CreateTests ) + "," + BoolToBitString( user.PermissionSettings.DeleteTests ) + "," + BoolToBitString( user.PermissionSettings.ModifyTests ) + "," + BoolToBitString( user.PermissionSettings.CreateAreas ) + "," + BoolToBitString( user.PermissionSettings.DeleteAreas ) + "," + BoolToBitString( user.PermissionSettings.ModifyAreas ) + ")"; SqlCommand cmd = new SqlCommand( userCommand, SqlDataConnection ); cmd.ExecuteNonQuery( ); return; }
public void VerifyThatTransactionSafeDeleteTableWorks() { var fromObject = new User { Id = Guid.NewGuid(), Name = "John Doe" }; var toObject = new Usergroup(); Assert.Throws<InvalidDataException>(() => DatabaseSession.Instance.Connector.DeleteTable(fromObject)); DatabaseSession.Instance.Connector.CreateTableWithColumns(fromObject); DatabaseSession.Instance.Connector.CreateTableWithColumns(toObject); DatabaseSession.Instance.Connector.CreatePrimaryKeyConstraint(fromObject); DatabaseSession.Instance.Connector.CreatePrimaryKeyConstraint(toObject); var transaction = DatabaseSession.Instance.CreateTransaction(); DatabaseSession.Instance.Connector.CreateForeignKeyConstraint(fromObject.GetType().GetProperty("Usergroup_Id"), fromObject, toObject.GetType().GetProperty(toObject.PrimaryKey), toObject, transaction); DatabaseSession.Instance.Connector.DeleteForeignKeyConstraint(fromObject.GetType().GetProperty("Usergroup_Id"), fromObject, toObject.GetType().GetProperty(toObject.PrimaryKey), toObject, transaction); DatabaseSession.Instance.Connector.DeleteTable(fromObject, transaction); Assert.IsTrue(DatabaseSession.Instance.Connector.CheckTableExists(fromObject)); DatabaseSession.Instance.Connector.DeleteTable(toObject, transaction); Assert.IsTrue(DatabaseSession.Instance.Connector.CheckTableExists(toObject)); DatabaseSession.Instance.CommitTransaction(transaction); Assert.IsFalse(DatabaseSession.Instance.Connector.CheckTableExists(fromObject)); Assert.IsFalse(DatabaseSession.Instance.Connector.CheckTableExists(toObject)); }
private static void AddUser( ) { string externalAccount = null; bool userAlreadyExists = false; Console.WriteLine( "\nADD USER: "******"\n" ); Console.WriteLine( "To add a user, you must associate the username with an external" ); Console.WriteLine( "account on the local machine or on a domain server. To do this," ); Console.WriteLine( "enter the username as \"DOMAIN\\USERNAME\" or as \"MACHINE\\USERNAME\"\n"); Console.Write( "External Account: " ); // Read in the external user account to use... externalAccount = Console.ReadLine( ); Permissions permissions = new Permissions( ); Console.WriteLine( "\nNow, permissions for this user must be set.\n" ); Console.Write( "Allow to Create Tests [y/n]? " ); permissions.CreateTests = ConvertResponseToBool( Console.ReadLine( ) ); Console.Write( "Allow to Delete Tests [y/n]? " ); permissions.DeleteTests = ConvertResponseToBool( Console.ReadLine( ) ); Console.Write( "Allow to Modify Tests [y/n]? " ); permissions.ModifyTests = ConvertResponseToBool( Console.ReadLine( ) ); Console.Write( "Allow to Create Areas [y/n]? " ); permissions.CreateAreas = ConvertResponseToBool( Console.ReadLine( ) ); Console.Write( "Allow to Delete Areas [y/n]? " ); permissions.DeleteAreas = ConvertResponseToBool( Console.ReadLine( ) ); Console.Write( "Allow to Modify Areas [y/n]? " ); permissions.ModifyAreas = ConvertResponseToBool( Console.ReadLine( ) ); User user = new User( ); user.PermissionSettings = permissions; user.ExternID = externalAccount.ToUpper( ); user.Name = UserName.ToUpper( ); Console.WriteLine( "\n**YOU ENTERED**" ); PrintUserInformation( user ); Console.Write( "\nDoes the above information look correct [y/n]?" ); try { if ( ConvertResponseToBool( Console.ReadLine( ) ) ) { SQLDatabase dbConnection = new SQLDatabase( ); dbConnection.Connect( ); try { dbConnection.GetUserIDFromName( user.Name ); userAlreadyExists = true; } catch ( Exception ) { // this is the SUCCESS case... } if ( userAlreadyExists ) throw new Exception( "User Already Exists!" ); dbConnection.AddUser( user ); dbConnection.Disconnect( ); Console.WriteLine( "\nTHE USER WAS ADDED SUCCESSFULLY" ); } else { Console.WriteLine( "\nPLEASE RUN THE TOOL AGAIN WITH THE PROPER INFORMATION" ); } } catch ( Exception ) { Console.WriteLine( "\nERROR: The user could not be added because the user already exists, or" ); Console.WriteLine( "you do not have access to the sql database." ); } }
/// <summary> /// Create a new User object. /// </summary> /// <param name="id">Initial value of the ID property.</param> /// <param name="username">Initial value of the Username property.</param> /// <param name="password">Initial value of the Password property.</param> /// <param name="admin">Initial value of the Admin property.</param> /// <param name="banned">Initial value of the Banned property.</param> public static User CreateUser(global::System.Int32 id, global::System.String username, global::System.String password, global::System.Byte admin, global::System.Boolean banned) { User user = new User(); user.ID = id; user.Username = username; user.Password = password; user.Admin = admin; user.Banned = banned; return user; }