summaryrefslogtreecommitdiff
path: root/Source/Kernel/UserManager/User.class.h
blob: ae022816bcd7c473a403e60de042924b03bf02f0 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
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
#ifndef DEF_USER_CLASS_H
#define DEF_USER_CLASS_H

#include <UserManager/Group.class.h>
#include <Vector.class.h>

class User {
	friend void Usr::load();

	private:
	String m_username, m_completeName;
	u32int m_uid;
	Group* m_group;
	Vector<Group*> m_extraGroups;
	
	User(String username, String completeName, Group* group, String extragroups, u32int uid)
		: m_username(username), m_completeName(completeName), m_uid(uid), m_group(group) {
		Vector<String> eg = extragroups.split(",");
		for (u32int i = 0; i < eg.size(); i++) {
			Group* g = Usr::group(eg[i].toInt());
			if (g != 0) m_extraGroups.push(g);
		}
	}

	public:
	String getUserName() { return m_username; }
	String getCompleteName() { return m_completeName; }
	u32int getUid() { return m_uid; }
	Group* getGroup() { return m_group; }
	bool isInGroup(u32int gid) {
		if (m_group->getGid() == gid) return true;
		for (u32int i = 0; i < m_extraGroups.size(); i++)
			if (m_extraGroups[i]->getGid() == gid) return true;
		return false;
	}
	bool isInGroup(String name) {
		if (m_group->getName() == name) return true;
		for (u32int i = 0; i < m_extraGroups.size(); i++)
			if (m_extraGroups[i]->getName() == name) return true;
		return false;
	}
	bool isInGroup(Group* g) {
		if (g == m_group) return true;
		for (u32int i = 0; i < m_extraGroups.size(); i++)
			if (m_extraGroups[i] == g) return true;
		return false;
	}

	String getGroups() {
		String ret;
		for (u32int i = 0; i < m_extraGroups.size(); i++) {
			if (!ret.empty()) ret += ",";
			ret += String::number(m_extraGroups[i]->getGid());
		}
		return ret;
	}

	void setUserName(String wat) { m_username = wat; Usr::save(); }
	void setCompleteName(String wat) { m_completeName = wat; Usr::save(); }
	void setGroup(Group* group) { m_group = group; Usr::save(); }
	void addGroup(u32int gid) {
		Group* g = Usr::group(gid);
		if (g != 0 and !isInGroup(g)) {
			m_extraGroups.push(g);
		}
		Usr::save();
	}
};

#endif