summaryrefslogtreecommitdiff
path: root/Source/Kernel/UserManager
diff options
context:
space:
mode:
authorAlexis211 <alexis211@gmail.com>2009-10-21 19:11:53 +0200
committerAlexis211 <alexis211@gmail.com>2009-10-21 19:11:53 +0200
commite70b7c569ba13a68aba1c2b127811e61ac88a902 (patch)
tree4fa6bb927e22b9664e172ebe9c74b10680df3036 /Source/Kernel/UserManager
parentdc37d089e8ca98ff2dc8a320c21fc3ac0a87eaa6 (diff)
downloadMelon-e70b7c569ba13a68aba1c2b127811e61ac88a902.tar.gz
Melon-e70b7c569ba13a68aba1c2b127811e61ac88a902.zip
Started working on user managment
Diffstat (limited to 'Source/Kernel/UserManager')
-rw-r--r--Source/Kernel/UserManager/Group.class.h22
-rw-r--r--Source/Kernel/UserManager/User.class.h67
-rw-r--r--Source/Kernel/UserManager/Usr.ns.cpp95
-rw-r--r--Source/Kernel/UserManager/Usr.ns.h23
4 files changed, 207 insertions, 0 deletions
diff --git a/Source/Kernel/UserManager/Group.class.h b/Source/Kernel/UserManager/Group.class.h
new file mode 100644
index 0000000..e515058
--- /dev/null
+++ b/Source/Kernel/UserManager/Group.class.h
@@ -0,0 +1,22 @@
+#ifndef DEF_GROUP_CLASS_H
+#define DEF_GROUP_CLASS_H
+
+#include <UserManager/Usr.ns.h>
+
+class Group {
+ friend void Usr::load();
+
+ private:
+ Group(String name, u32int gid) : m_name(name), m_gid(gid) {}
+
+ String m_name;
+ u32int m_gid;
+
+ public:
+ void setName(String wat) { m_name = wat; Usr::save(); }
+
+ String getName() { return m_name; }
+ u32int getGid() { return m_gid; }
+};
+
+#endif
diff --git a/Source/Kernel/UserManager/User.class.h b/Source/Kernel/UserManager/User.class.h
new file mode 100644
index 0000000..1ad55f4
--- /dev/null
+++ b/Source/Kernel/UserManager/User.class.h
@@ -0,0 +1,67 @@
+#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) {
+ for (u32int i = 0; i < m_extraGroups.size(); i++)
+ if (m_extraGroups[i]->getGid() == gid) return true;
+ return false;
+ }
+ bool isInGroup(String name) {
+ for (u32int i = 0; i < m_extraGroups.size(); i++)
+ if (m_extraGroups[i]->getName() == name) return true;
+ return false;
+ }
+ bool isInGroup(Group* g) {
+ 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
diff --git a/Source/Kernel/UserManager/Usr.ns.cpp b/Source/Kernel/UserManager/Usr.ns.cpp
new file mode 100644
index 0000000..a551344
--- /dev/null
+++ b/Source/Kernel/UserManager/Usr.ns.cpp
@@ -0,0 +1,95 @@
+#include "Usr.ns.h"
+
+#include <UserManager/User.class.h>
+#include <SimpleList.class.h>
+#include <TaskManager/Task.ns.h>
+#include <VFS/TextFile.class.h>
+
+/*
+ * Syntax for Users and Groups configuration files : one entry per line
+ * syntax for Users : <uid>:<username>:<basegroup>:<extragroup>,<extragroup>:<completename>
+ * syntax for Groups : <gid>:<name>
+ */
+
+namespace Usr {
+
+SimpleList <User> *m_users = 0;
+SimpleList <Group> *m_groups = 0;
+
+void load() {
+ if (m_users != 0) delete m_users;
+ if (m_groups != 0) delete m_groups;
+ m_users = 0, m_groups = 0;
+
+ TextFile groups("/System/Configuration/Groups", FM_READ);
+ while (!groups.eof()) {
+ String s = groups.readLine();
+ Vector<String> data = s.split(":");
+ if (data.size() == 2 and !(s[0] == WChar("#"))) {
+ m_groups = m_groups->cons(Group(data[1], data[0].toInt()));
+ }
+ }
+
+ TextFile users("/System/Configuration/Users", FM_READ);
+ while (!users.eof()) {
+ String s = users.readLine();
+ Vector<String> data = s.split(":");
+ if (data.size() == 5 and !(s[0] == WChar("#"))) {
+ m_users = m_users->cons(User(data[1], data[4], group(data[2].toInt()), data[3], data[0].toInt()));
+ }
+ }
+}
+
+void save() {
+ TextFile groups("/System/Configuration/Groups", FM_TRUNCATE);
+ for (SimpleList<Group> *iter = m_groups; iter != 0; iter = iter->next()) {
+ groups.write(String::number(iter->v().getGid()) + ":" + iter->v().getName(), true);
+ }
+ TextFile users("/System/Configuration/Users", FM_TRUNCATE);
+ for (SimpleList<User> *iter = m_users; iter != 0; iter = iter->next()) {
+ users.write(String::number(iter->v().getUid()) + ":" + iter->v().getUserName() + ":"
+ + String::number(iter->v().getGroup()->getGid()) + ":"
+ + iter->v().getGroups() + ":" + iter->v().getCompleteName(), true);
+ }
+}
+
+u32int uid() {
+ return Task::currProcess()->getUid();
+}
+
+User* user(u32int uid) {
+ for (SimpleList<User> *iter = m_users; iter != 0; iter = iter->next()) {
+ if (iter->v().getUid() == uid) return &iter->v();
+ }
+ return 0;
+}
+
+User* user(String username) {
+ for (SimpleList<User> *iter = m_users; iter != 0; iter = iter->next()) {
+ if (iter->v().getUserName() == username) return &iter->v();
+ }
+ return 0;
+}
+
+User* user() { return user(uid()); }
+
+Group* group(u32int gid) {
+ for (SimpleList<Group> *iter = m_groups; iter != 0; iter = iter->next()) {
+ if (iter->v().getGid() == gid) return &iter->v();
+ }
+ return 0;
+}
+
+Group* group(String name) {
+ for (SimpleList<Group> *iter = m_groups; iter != 0; iter = iter->next()) {
+ if (iter->v().getName() == name) return &iter->v();
+ }
+ return 0;
+}
+
+u32int uid(String username) {
+ User* x = user(username);
+ return (x != 0 ? x->getUid() : (u32int) - 1);
+}
+
+};
diff --git a/Source/Kernel/UserManager/Usr.ns.h b/Source/Kernel/UserManager/Usr.ns.h
new file mode 100644
index 0000000..397b393
--- /dev/null
+++ b/Source/Kernel/UserManager/Usr.ns.h
@@ -0,0 +1,23 @@
+#ifndef DEF_USR_NS_H
+#define DEF_USR_NS_H
+
+#include <String.class.h>
+class Group;
+class User;
+
+namespace Usr {
+ void load(); //Loads users into memory, from /System/Configuration/{Users,Groups}
+ void save(); //Saves config from mem to filesystem
+
+ u32int uid(); //Returns current processes UID
+
+ User* user(u32int uid); //Returns user from UID
+ User* user(String username);
+ User* user();
+ Group* group(u32int gid);
+ Group* group(String name);
+
+ u32int uid(String username); //Returns UID of username
+};
+
+#endif