Пример #1
0
        public bool AddGroupPermission(int groupId, string permission, bool allowed = true)
        {
            SharpStarGroup group = GetGroup(groupId);

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

            using (var session = CreateSession())
            {
                using (var transaction = session.BeginTransaction())
                {
                    if (session.Query <SharpStarGroupPermission>().Any(p => p.Group.Id == group.Id && p.Permission.ToLower() == permission.ToLower()))
                    {
                        return(false);
                    }

                    var perm = new SharpStarGroupPermission
                    {
                        Group      = group,
                        Permission = permission,
                        Allowed    = allowed
                    };

                    session.Save(perm);

                    transaction.Commit();
                }
            }

            return(true);
        }
Пример #2
0
        public bool SetDefaultGroup(int groupId)
        {
            using (var session = CreateSession())
            {
                using (var transaction = session.BeginTransaction())
                {
                    SharpStarGroup defGroup = session.Query <SharpStarGroup>().SingleOrDefault(p => p.IsDefaultGroup);

                    if (defGroup != null)
                    {
                        defGroup.IsDefaultGroup = false;

                        session.SaveOrUpdate(defGroup);
                    }

                    SharpStarGroup group = GetGroup(groupId);

                    if (group != null)
                    {
                        group.IsDefaultGroup = true;

                        session.SaveOrUpdate(group);

                        transaction.Commit();

                        return(true);
                    }
                }
            }

            return(false);
        }
Пример #3
0
        public bool RemoveGroupPermission(string groupName, string permission)
        {
            SharpStarGroup group = GetGroup(groupName);

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

            return(RemoveGroupPermission(group.Id, permission));
        }
Пример #4
0
        public bool AddGroupPermission(string groupName, string permission, bool allowed = true)
        {
            SharpStarGroup group = GetGroup(groupName);

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

            return(AddGroupPermission(group.Id, permission, allowed));
        }
Пример #5
0
        public List <SharpStarGroupPermission> GetGroupPermissions(int groupId)
        {
            using (var session = CreateSession())
            {
                SharpStarGroup group = session.Get <SharpStarGroup>(groupId);

                if (group == null)
                {
                    return(null);
                }

                return(group.Permissions.ToList());
            }
        }
Пример #6
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);
                }
            }
        }
Пример #7
0
        public SharpStarGroup CreateGroup(string groupName, bool defaultGroup = false)
        {
            using (var session = CreateSession())
            {
                using (var transaction = session.BeginTransaction())
                {
                    if (session.Query <SharpStarGroup>().Any(p => p.GroupName.ToLower() == groupName.ToLower()))
                    {
                        return(null);
                    }

                    if (defaultGroup)
                    {
                        var defGroup = session.Query <SharpStarGroup>().SingleOrDefault(p => p.IsDefaultGroup);

                        if (defGroup != null)
                        {
                            defGroup.IsDefaultGroup = false;

                            session.SaveOrUpdate(defGroup);
                        }
                    }

                    var group = new SharpStarGroup
                    {
                        GroupName      = groupName,
                        IsDefaultGroup = defaultGroup
                    };

                    session.SaveOrUpdate(group);

                    transaction.Commit();

                    return(group);
                }
            }
        }
Пример #8
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();
                }
            }
        }