Account.java 5.4 KB
Newer Older
Melledy's avatar
Melledy committed
1
2
package emu.grasscutter.game;

3
import dev.morphia.annotations.*;
Melledy's avatar
Melledy committed
4
5
6
7
import emu.grasscutter.database.DatabaseHelper;
import emu.grasscutter.utils.Crypto;
import emu.grasscutter.utils.Utils;

8
9
import java.util.*;
import java.util.stream.Stream;
10

11
12
import org.bson.Document;

13
import static emu.grasscutter.Configuration.*;
14

KingRainbow44's avatar
KingRainbow44 committed
15
@Entity(value = "accounts", useDiscriminator = false)
Melledy's avatar
Melledy committed
16
17
18
19
20
21
22
23
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
	
24
	private int reservedPlayerId;
Melledy's avatar
Melledy committed
25
26
27
28
	private String email;
	
	private String token;
	private String sessionKey; // Session token for dispatch server
29
	private List<String> permissions;
Secretboy's avatar
Secretboy committed
30
    private Locale locale;
Yazawazi's avatar
Yazawazi committed
31
32
33
34
35

	private String banReason;
	private int banEndTime;
	private int banStartTime;
	private boolean isBanned;
36
	
Melledy's avatar
Melledy committed
37
	@Deprecated
Melledy's avatar
Melledy committed
38
39
	public Account() {
		this.permissions = new ArrayList<>();
40
        this.locale = LANGUAGE;
Melledy's avatar
Melledy committed
41
	}
Melledy's avatar
Melledy committed
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

	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;
	}

75
76
	public int getReservedPlayerUid() {
		return this.reservedPlayerId;
Melledy's avatar
Melledy committed
77
78
	}

79
80
	public void setReservedPlayerUid(int playerId) {
		this.reservedPlayerId = playerId;
Melledy's avatar
Melledy committed
81
82
83
	}
	
	public String getEmail() {
4Benj_'s avatar
4Benj_ committed
84
85
86
87
88
		if(email != null && !email.isEmpty()) {
			return email;
		} else {
			return "";
		}
Melledy's avatar
Melledy committed
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
	}

	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;
	}
104

Secretboy's avatar
Secretboy committed
105
106
107
108
109
110
111
112
    public Locale getLocale() {
        return locale;
    }

    public void setLocale(Locale locale) {
        this.locale = locale;
    }

Yazawazi's avatar
Yazawazi committed
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
	public String getBanReason() {
		return banReason;
	}

	public void setBanReason(String banReason) {
		this.banReason = banReason;
	}

	public int getBanEndTime() {
		return banEndTime;
	}

	public void setBanEndTime(int banEndTime) {
		this.banEndTime = banEndTime;
	}

	public int getBanStartTime() {
		return banStartTime;
	}

	public void setBanStartTime(int banStartTime) {
		this.banStartTime = banStartTime;
	}

	public boolean isBanned() {
		if (banEndTime > 0 && banEndTime < System.currentTimeMillis() / 1000) {
			this.isBanned = false;
			this.banEndTime = 0;
			this.banStartTime = 0;
			this.banReason = null;
			save();
		}

		return isBanned;
	}

	public void setBanned(boolean isBanned) {
		this.isBanned = isBanned;
	}

153
154
155
156
157
158
159
160
161
162
163
	/**
	 * 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;
	}
164

AnimeGitB's avatar
AnimeGitB committed
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
	public static boolean permissionMatchesWildcard(String wildcard, String[] permissionParts) {
		String[] wildcardParts = wildcard.split("\\.");
		if (permissionParts.length < wildcardParts.length) {  // A longer wildcard can never match a shorter permission
			return false;
		}
		for (int i=0; i<wildcardParts.length; i++) {
			switch (wildcardParts[i]) {
				case "**":  // Recursing match
					return true;
				case "*":  // Match only one layer
					if (i >= (permissionParts.length-1)) {
						return true;
					}
					break;
				default:  // This layer isn't a wildcard, it needs to match exactly
					if (!wildcardParts[i].equals(permissionParts[i])) {
						return false;
					}
			}
		}
		// At this point the wildcard will have matched every layer, but if it is shorter then the permission then this is not a match at this point (no **).
		return (wildcardParts.length == permissionParts.length);
	}

189
	public boolean hasPermission(String permission) {
muhammadeko's avatar
muhammadeko committed
190
		if(this.permissions.contains("*") && this.permissions.size() == 1) return true;
muhammadeko's avatar
muhammadeko committed
191

192
193
194
195
196
197
198
		// Add default permissions if it doesn't exist
		List<String> permissions = Stream.of(this.permissions, Arrays.asList(ACCOUNT.defaultPermissions))
				.flatMap(Collection::stream)
				.distinct().toList();

		if (permissions.contains(permission)) return true;

AnimeGitB's avatar
AnimeGitB committed
199
		String[] permissionParts = permission.split("\\.");
200
		for (String p : permissions) {
muhammadeko's avatar
muhammadeko committed
201
			if (p.startsWith("-") && permissionMatchesWildcard(p.substring(1), permissionParts)) return false;
muhammadeko's avatar
muhammadeko committed
202
			if (permissionMatchesWildcard(p, permissionParts)) return true;
AnimeGitB's avatar
AnimeGitB committed
203
		}
muhammadeko's avatar
muhammadeko committed
204

205
		return permissions.contains("*");
206
	}
207

208
209
210
	public boolean removePermission(String permission) {
		return this.permissions.remove(permission);
	}
211

Melledy's avatar
Melledy committed
212
213
214
215
216
217
218
219
220
221
	// TODO make unique
	public String generateLoginToken() {
		this.token = Utils.bytesToHex(Crypto.createSessionKey(32));
		this.save();
		return this.token;
	}
	
	public void save() {
		DatabaseHelper.saveAccount(this);
	}
222

223
	@PreLoad
224
	public void onLoad(Document document) {
225
		// Grant the superuser permissions to accounts created before the permissions update
226
		if (!document.containsKey("permissions")) {
227
228
			this.addPermission("*");
		}
Secretboy's avatar
Secretboy committed
229
230
231

        // Set account default language as server default language
        if (!document.containsKey("locale")) {
232
            this.locale = LANGUAGE;
Secretboy's avatar
Secretboy committed
233
        }
234
	}
Melledy's avatar
Melledy committed
235
}