forked from colegarien/SDDTermProject
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request colegarien#6 from nelalison/Nelson_SDD_database
db create and get user
- Loading branch information
Showing
3 changed files
with
235 additions
and
0 deletions.
There are no files selected for viewing
111 changes: 111 additions & 0 deletions
111
app/src/main/java/edu/uco/schambers/classmate/Database/DataRepo.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,111 @@ | ||
package edu.uco.schambers.classmate.Database; | ||
|
||
import android.content.ContentValues; | ||
import android.content.Context; | ||
import android.database.Cursor; | ||
import android.database.sqlite.SQLiteDatabase; | ||
import android.database.sqlite.SQLiteOpenHelper; | ||
import android.util.Log; | ||
|
||
import java.util.ArrayList; | ||
import java.util.List; | ||
|
||
|
||
public class DataRepo extends SQLiteOpenHelper { | ||
private static final int DATABASE_VERSION = 4; | ||
private static final String DATABASE_NAME = "CLASSMATE_TABLES"; | ||
private static final String CREATE_TABLE_USER = "create table users " + | ||
"(id integer primary key, username " + | ||
"text, password text, fname text,lname text , " + | ||
"isstudent integer, isstaff integer, phone text, " + | ||
"email text, ismale integer)"; | ||
|
||
private static final String Role = "create table dbRoles " + | ||
"user_id int not null, " + | ||
"group text, " + | ||
"primary key(user_id, group),"+ | ||
"CONSTRAINT user_fkey Foreign key(user_id) references users(id) ON DELETE CASCADE ON UPDATE RESTRICT"+ | ||
")"; | ||
|
||
public DataRepo(Context context) { | ||
super(context, DATABASE_NAME, null, DATABASE_VERSION); | ||
} | ||
|
||
@Override | ||
public void onCreate(SQLiteDatabase db) { | ||
db.execSQL(CREATE_TABLE_USER); | ||
db.execSQL(Role); | ||
} | ||
|
||
@Override | ||
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) { | ||
Log.d("DEBUG", "Database version has changed to version: " + DATABASE_VERSION); | ||
db.execSQL("DROP TABLE IF EXISTS users"); | ||
db.execSQL("DROP TABLE IF EXISTS dbRoles"); | ||
onCreate(db); | ||
} | ||
|
||
|
||
public void createUser(User user) { | ||
SQLiteDatabase db = this.getWritableDatabase(); | ||
ContentValues contentValues = new ContentValues(); | ||
|
||
contentValues.put("username", user.getUsername()); | ||
contentValues.put("password", user.getPassword()); | ||
contentValues.put("fname", user.getFname()); | ||
contentValues.put("lname", user.getLname()); | ||
contentValues.put("isstudent", user.isStudent() ? 1 : 0); | ||
contentValues.put("isstaff", user.isStaff() ? 1 : 0); | ||
contentValues.put("language", user.getPhone()); | ||
contentValues.put("race", user.getEmail()); | ||
contentValues.put("ismale", user.isMale() ? 1 : 0); | ||
|
||
db.insert("users", null, contentValues); | ||
} | ||
|
||
public User getUser(String username) { | ||
SQLiteDatabase db = this.getReadableDatabase(); | ||
Cursor res = db.rawQuery( "select * from users where username = '" + username + "'", null); | ||
|
||
if (res.moveToNext()) | ||
{ | ||
User user = new User(); | ||
|
||
user.setId(res.getInt(0)); | ||
user.setUsername(res.getString(1)); | ||
user.setPassword(res.getString(2)); | ||
user.setFname(res.getString(3)); | ||
user.setLname(res.getString(4)); | ||
user.setIsStudent(res.getInt(5) == 1); | ||
user.setIsStaff(res.getInt(6) == 1); | ||
user.setPhone(res.getString(7)); | ||
user.setEmail(res.getString(8)); | ||
user.setIsMale(res.getInt(9) == 1); | ||
|
||
return user; | ||
} | ||
|
||
return null; | ||
} | ||
|
||
public boolean validateUser(String username, String password) { | ||
SQLiteDatabase db = this.getReadableDatabase(); | ||
Cursor res = db.rawQuery( "select * from users where username = '" + username + "' and password = '" + password + "'", null); | ||
|
||
if (res.getCount() > 0) | ||
return true; | ||
|
||
return false; | ||
} | ||
|
||
public boolean validateUser(String password) { | ||
SQLiteDatabase db = this.getReadableDatabase(); | ||
Cursor res = db.rawQuery( "select * from users where password = '" + password + "'", null); | ||
|
||
if (res.getCount() > 0) | ||
return true; | ||
|
||
return false; | ||
} | ||
|
||
} |
26 changes: 26 additions & 0 deletions
26
app/src/main/java/edu/uco/schambers/classmate/Database/SHA256Encrypt.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
package edu.uco.schambers.classmate.Database; | ||
|
||
|
||
|
||
|
||
|
||
import java.io.UnsupportedEncodingException; | ||
import java.math.BigInteger; | ||
import java.security.MessageDigest; | ||
import java.security.NoSuchAlgorithmException; | ||
|
||
public class SHA256Encrypt { | ||
|
||
public static String encrypt(String data) { | ||
try { | ||
MessageDigest md = MessageDigest.getInstance("SHA-256"); | ||
md.update(data.getBytes("UTF-8")); // Change this to "UTF-16" if needed | ||
byte[] digest = md.digest(); | ||
BigInteger bigInt = new BigInteger(1, digest); | ||
return bigInt.toString(16); | ||
} catch (NoSuchAlgorithmException | UnsupportedEncodingException e) { | ||
return null; | ||
} | ||
} | ||
|
||
} |
98 changes: 98 additions & 0 deletions
98
app/src/main/java/edu/uco/schambers/classmate/Database/User.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,98 @@ | ||
package edu.uco.schambers.classmate.Database; | ||
|
||
import java.io.Serializable; | ||
|
||
|
||
public class User implements Serializable { | ||
public int id; | ||
public String username; | ||
public String password; | ||
public String fname; | ||
public String lname; | ||
public boolean Student; | ||
public boolean Staff; | ||
public String phone; | ||
public String email; | ||
public boolean Male; | ||
|
||
public int getId() { | ||
return id; | ||
} | ||
|
||
public void setId(int id) { | ||
this.id = id; | ||
} | ||
|
||
public String getUsername() { | ||
return username; | ||
} | ||
|
||
public void setUsername(String username) { | ||
this.username = username; | ||
} | ||
|
||
public String getPassword() { | ||
return password; | ||
} | ||
|
||
public void setPassword(String password) { | ||
this.password = SHA256Encrypt.encrypt(password); | ||
} | ||
|
||
public String getFname() { | ||
return fname; | ||
} | ||
|
||
public void setFname(String fname) { | ||
this.fname = fname; | ||
} | ||
|
||
public String getLname() { | ||
return lname; | ||
} | ||
|
||
public void setLname(String lname) { | ||
this.lname = lname; | ||
} | ||
|
||
public boolean isStudent() { | ||
return Student; | ||
} | ||
|
||
public void setIsStudent(boolean Student) { | ||
this.Student = Student; | ||
} | ||
|
||
public boolean isStaff() { | ||
return Staff; | ||
} | ||
|
||
public void setIsStaff(boolean Staff) { | ||
this.Staff = Staff; | ||
} | ||
|
||
public String getPhone() { | ||
return phone; | ||
} | ||
|
||
public void setPhone(String phone) { | ||
this.phone = phone; | ||
} | ||
|
||
public String getEmail() { | ||
return email; | ||
} | ||
|
||
public void setEmail(String email) { | ||
this.email = email; | ||
} | ||
|
||
public boolean isMale() { | ||
return Male; | ||
} | ||
|
||
public void setIsMale(boolean Male) { | ||
this.Male = Male; | ||
} | ||
|
||
} |