Exemplo n.º 1
0
        public bool UpdateUserPassword(string username, string password)
        {
            using (var session = CreateSession())
            {
                using (var transaction = session.BeginTransaction())
                {
                    SharpStarUser user = session.Query <SharpStarUser>().SingleOrDefault(p => p.Username.ToLower() == username.ToLower());

                    if (user == null)
                    {
                        return(false);
                    }

                    string salt = SharpStarSecurity.GenerateSalt();

                    user.Hash = SharpStarSecurity.GenerateHash(username, password, salt, StarboundConstants.Rounds);
                    user.Salt = salt;

                    session.SaveOrUpdate(user);

                    transaction.Commit();
                }
            }

            return(true);
        }
Exemplo n.º 2
0
        public void ChangeAdminStatus(int userId, bool admin)
        {
            using (var session = CreateSession())
            {
                using (var transaction = session.BeginTransaction())
                {
                    SharpStarUser user = GetUser(userId);

                    if (user != null)
                    {
                        user.IsAdmin = admin;

                        session.SaveOrUpdate(user);

                        transaction.Commit();
                    }
                }
            }
        }
Exemplo n.º 3
0
        public bool DeleteUser(string username)
        {
            using (var session = CreateSession())
            {
                using (var transaction = session.BeginTransaction())
                {
                    SharpStarUser user = session.Query <SharpStarUser>().SingleOrDefault(p => p.Username.ToLower() == username.ToLower());

                    if (user == null)
                    {
                        return(false);
                    }

                    session.Delete(user);

                    transaction.Commit();
                }
            }

            return(true);
        }
Exemplo n.º 4
0
        public bool AddUser(string username, string password, bool admin = false, int?groupId = null)
        {
            using (var session = CreateSession())
            {
                using (var transaction = session.BeginTransaction())
                {
                    SharpStarUser user = session.Query <SharpStarUser>().SingleOrDefault(p => p.Username.ToLower() == username.ToLower());

                    if (user == null)
                    {
                        string salt = SharpStarSecurity.GenerateSalt();

                        SharpStarGroup group = null;
                        if (!groupId.HasValue)
                        {
                            SharpStarGroup defaultGroup = session.Query <SharpStarGroup>().SingleOrDefault(p => p.IsDefaultGroup);

                            if (defaultGroup != null)
                            {
                                group = defaultGroup;
                            }
                        }
                        else
                        {
                            group = session.Query <SharpStarGroup>().SingleOrDefault();
                        }

                        session.Save(new SharpStarUser {
                            Username = username, Hash = SharpStarSecurity.GenerateHash(username, password, salt, 5000), Salt = salt, IsAdmin = admin, Group = group
                        });

                        transaction.Commit();
                    }

                    return(user == null);
                }
            }
        }
Exemplo n.º 5
0
        public bool UpdateUserLastLogin(string username, DateTime lastLogin)
        {
            using (var session = CreateSession())
            {
                using (var transaction = session.BeginTransaction())
                {
                    SharpStarUser user = session.Query <SharpStarUser>().SingleOrDefault(p => p.Username.ToLower() == username.ToLower());

                    if (user == null)
                    {
                        return(false);
                    }

                    user.LastLogin = lastLogin;

                    session.SaveOrUpdate(user);

                    transaction.Commit();
                }
            }

            return(true);
        }
Exemplo n.º 6
0
        public static void Migrate(string oldDb, string newDb)
        {
            using (var session = SharpStarMain.Instance.Database.CreateSession())
            {
                using (var transaction = session.BeginTransaction())
                {
                    var conn = new SQLiteConnection(oldDb);

                    var usrTbl     = conn.Table <OldDb.SharpStarUser>();
                    var grpTbl     = conn.Table <OldDb.SharpStarGroup>();
                    var usrPermTbl = conn.Table <OldDb.SharpStarPermission>();
                    var grpPermTbl = conn.Table <OldDb.SharpStarGroupPermission>();

                    foreach (var usr in usrTbl)
                    {
                        SharpStarUser newUsr = new SharpStarUser
                        {
                            Username  = usr.Username,
                            Salt      = usr.Salt,
                            Hash      = usr.Hash,
                            IsAdmin   = usr.IsAdmin,
                            LastLogin = usr.LastLogin
                        };

                        session.Save(newUsr);

                        int usrId = usr.Id;
                        foreach (var usrPerm in usrPermTbl.Where(p => p.UserId == usrId))
                        {
                            session.Save(new SharpStarPermission
                            {
                                Permission = usrPerm.Permission,
                                Allowed    = usrPerm.Allowed,
                                User       = newUsr
                            });
                        }
                    }

                    foreach (var grp in grpTbl)
                    {
                        var newGrp = new SharpStarGroup
                        {
                            GroupName      = grp.GroupName,
                            IsDefaultGroup = grp.IsDefaultGroup
                        };

                        session.Save(newGrp);

                        int grpId = grp.Id;
                        foreach (var usr in usrTbl.Where(p => p.GroupId == grpId))
                        {
                            string usrName = usr.Username;
                            var    newUsr  = session.Query <SharpStarUser>().SingleOrDefault(p => p.Username == usrName);

                            if (newUsr != null)
                            {
                                newUsr.Group = newGrp;
                            }
                        }

                        foreach (var grpPerm in grpPermTbl.Where(p => p.GroupId == grpId))
                        {
                            session.Save(new SharpStarGroupPermission
                            {
                                Permission = grpPerm.Permission,
                                Allowed    = grpPerm.Allowed,
                                Group      = newGrp
                            });
                        }
                    }

                    transaction.Commit();

                    conn.Close();
                    conn.Dispose();
                }
            }
        }