Account.java 2.84 KB
Newer Older
Melledy's avatar
Melledy committed
1
2
3
4
5
6
package emu.grasscutter.game;

import dev.morphia.annotations.Collation;
import dev.morphia.annotations.Entity;
import dev.morphia.annotations.Id;
import dev.morphia.annotations.Indexed;
7
import dev.morphia.annotations.PreLoad;
Melledy's avatar
Melledy committed
8
9
10
11
12
import emu.grasscutter.database.DatabaseHelper;
import emu.grasscutter.utils.Crypto;
import emu.grasscutter.utils.Utils;
import dev.morphia.annotations.IndexOptions;

Melledy's avatar
Melledy committed
13
import java.util.ArrayList;
14
15
import java.util.List;

16
17
import com.mongodb.DBObject;

Melledy's avatar
Melledy committed
18
19
20
21
22
23
24
25
26
27
28
29
30
31
@Entity(value = "accounts", noClassnameStored = true)
public class Account {
	@Id private String id;
	
	@Indexed(options = @IndexOptions(unique = true))
	@Collation(locale = "simple", caseLevel = true)
	private String username;
	private String password; // Unused for now
	
	private int playerId;
	private String email;
	
	private String token;
	private String sessionKey; // Session token for dispatch server
32
	private List<String> permissions;
Melledy's avatar
Melledy committed
33
34

	@Deprecated
Melledy's avatar
Melledy committed
35
36
37
	public Account() {
		this.permissions = new ArrayList<>();
	}
Melledy's avatar
Melledy committed
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95

	public String getId() {
		return id;
	}

	public void setId(String 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 = password;
	}

	public String getToken() {
		return token;
	}

	public void setToken(String token) {
		this.token = token;
	}

	public int getPlayerId() {
		return this.playerId;
	}

	public void setPlayerId(int playerId) {
		this.playerId = playerId;
	}
	
	public String getEmail() {
		return email;
	}

	public void setEmail(String email) {
		this.email = email;
	}

	public String getSessionKey() {
		return this.sessionKey;
	}

	public String generateSessionKey() {
		this.sessionKey = Utils.bytesToHex(Crypto.createSessionKey(32));
		this.save();
		return this.sessionKey;
	}
96
97
98
99
100
101
102
103
104
105
106
107

	/**
	 * The collection of a player's permissions.
	 */
	public List<String> getPermissions() {
		return this.permissions;
	}
	
	public boolean addPermission(String permission) {
		if(this.permissions.contains(permission)) return false;
		this.permissions.add(permission); return true;
	}
108
109
110
111

	public boolean hasPermission(String permission) {
		return this.permissions.contains(permission) || this.permissions.contains("*") ? true : false;
	}
112
113
114
115
	
	public boolean removePermission(String permission) {
		return this.permissions.remove(permission);
	}
Melledy's avatar
Melledy committed
116
117
118
119
120
121
122
123
	
	// TODO make unique
	public String generateLoginToken() {
		this.token = Utils.bytesToHex(Crypto.createSessionKey(32));
		this.save();
		return this.token;
	}
	
124
125
126
127
128
129
130
131
	@PreLoad
	public void onLoad(DBObject dbObj) {
		// Grant the superuser permissions to accounts created before the permissions update
		if (!dbObj.containsField("permissions")) {
			this.addPermission("*");
		}
	}
	
Melledy's avatar
Melledy committed
132
133
134
135
	public void save() {
		DatabaseHelper.saveAccount(this);
	}
}