public void ReturnBook(int id, User client) { if (client.Equals(GetBook(id).GetUser())) { this.GetBook(id).SetUser(null); } }
public string output(string input, User user) { Sentence sentence = Query.parse(input); string output = ""; if (sentence is Statements) { Statements statement = sentence as Statements; if (statement is Select) { Select sel = statement as Select; IList <string> columnsNames = sel.listColumns; string tableName = sel.tableName; Where where = sel.sentenceWhere; Operator op = where.op; string columnName = where.col; string dataToCompare = where.colData; if (hasPrivilege(user, tableName, Privilege.SELECT)) { output = select(columnsNames, tableName, columnName, op, dataToCompare).selectToString(); } else { output = Constants.SecurityNotSufficientPrivileges; } } else if (statement is SelectAll) { SelectAll sel = statement as SelectAll; IList <string> columnsNames = sel.listColumns; string tableName = sel.tableName; if (hasPrivilege(user, tableName, Privilege.SELECT)) { output = selectAll(columnsNames, tableName).selectToString(); } else { output = Constants.SecurityNotSufficientPrivileges; } } else if (statement is Delete) { Delete delete = statement as Delete; string tabName = delete.tableName; Where where = delete.sentenceWhere; string column = where.col; Operator op = where.op; string data = where.colData; if (hasPrivilege(user, tabName, Privilege.DELETE)) { deleteData(tabName, column, op, data); output = Constants.TupleDeleteSuccess; } else { output = Constants.SecurityNotSufficientPrivileges; } } else if (statement is Insert) { Insert ins = statement as Insert; string nameTable = ins.tableName; List <string> dataToInsert = ins.row; if (hasPrivilege(user, nameTable, Privilege.INSERT)) { insert(nameTable, dataToInsert); output = Constants.InsertSuccess; } else { output = Constants.SecurityNotSufficientPrivileges; } } else if (statement is Update) { Update upd = statement as Update; string tableName = upd.tableName; List <string> columnNames = upd.column; List <string> newValues = upd.newValue; Where where = upd.sentenceWhere; string columnToCompare = where.col; Operator op = where.op; string data = where.colData; if (hasPrivilege(user, tableName, Privilege.UPDATE)) { for (int i = 0; i < columnNames.Count; i++) { string columnName = columnNames[i]; string newData = newValues[i]; update(tableName, columnName, newData, columnToCompare, op, data); } output = Constants.TupleUpdateSuccess; } else { output = Constants.SecurityNotSufficientPrivileges; } } else if (statement is DropTable) { DropTable drop = statement as DropTable; string tableName = drop.tableName; if (user.Name.Equals("admin")) { dropTable(tableName); output = Constants.TableDroppedSucess; } else { output = Constants.SecurityNotSufficientPrivileges; } } else if (statement is CreateTable) { CreateTable create = statement as CreateTable; string tableName = create.tableName; List <string> colNames = create.ListOfColumns; if (user.Name.Equals("admin")) { createTable(tableName, colNames); output = Constants.CreateTableSuccess; } else { output = Constants.SecurityNotSufficientPrivileges; } } } else if (sentence is SecurityQueries) { SecurityQueries securityQueries = sentence as SecurityQueries; if (!user.Equals(admin.Name) && !user.Password.Equals(admin.Password)) { output = Constants.SecurityNotSufficientPrivileges; } else { if (securityQueries is AddUser) { AddUser addUseer = securityQueries as AddUser; string name = addUseer.User; string pass = addUseer.Password; string security = addUseer.SecurityProfileName; addUser(name, pass, security); output = Constants.SecurityUserAdded; } else if (securityQueries is DeleteUser) { DeleteUser deleteeUser = securityQueries as DeleteUser; string name = deleteeUser.User; deleteUser(name); output = Constants.SecurityUserDeleted; } else if (securityQueries is CreateSecurityProfile) { CreateSecurityProfile create = securityQueries as CreateSecurityProfile; string security = create.SecurityProfileName; createSecurityProfile(security); output = Constants.SecurityProfileCreated; } else if (securityQueries is DropSecurityProfile) { DropSecurityProfile drop = securityQueries as DropSecurityProfile; string name = drop.SecurityProfileName; dropSecurityProfile(name); output = Constants.SecurityProfileDeleted; } else if (securityQueries is GrantPrivilege) { GrantPrivilege graant = securityQueries as GrantPrivilege; Privilege type = graant.Type; string table = graant.Table; string security = graant.SecurityProfileName; grant(type, table, security); output = Constants.SecurityPrivilegeGranted; } else if (securityQueries is RevokePrivilege) { RevokePrivilege revooke = securityQueries as RevokePrivilege; Privilege type = revooke.Type; string table = revooke.Table; string security = revooke.SecurityProfileName; revoke(type, table, security); output = Constants.SecurityPrivilegeRevoked; } } } else { throw new Exception(Constants.WrongSyntax); } return(output); }