Ejemplo n.º 1
0
        /// <summary>
        /// Fetches the setting by key
        /// </summary>
        /// <returns>The string.</returns>
        /// <param name="key">Key.</param>
        public static string GetString(string key)
        {
            using (var ctx = new OTAContext())
            {
                var stg = ctx.Settings.SingleOrDefault(x => x.DataKey == key);

                if (stg != null) return stg.DataValue;
            }

            return null;
        }
        /// <summary>
        /// Removes a user from the database by name
        /// </summary>
        /// <returns><c>true</c>, if user was deleted, <c>false</c> otherwise.</returns>
        /// <param name="username">Username.</param>
        public static bool DeleteUser(string username)
        {
            using (var ctx = new OTAContext())
            {
                var matches = ctx.Players.Where(x => x.Name == username);
                var range = ctx.Players.RemoveRange(matches);

                ctx.SaveChanges();

                return matches.Any();
            }
        }
        /// <summary>
        /// Creates a user.
        /// </summary>
        /// <returns><c>true</c>, if user was created, <c>false</c> otherwise.</returns>
        /// <param name="username">Username.</param>
        /// <param name="password">Password.</param>
        /// <param name="op">If set to <c>true</c> op.</param>
        public static DbPlayer CreateUser(string username, string password, bool op = false)
        {
            using (var ctx = new OTAContext())
            {
                var player = ctx.Players.Add(new DbPlayer(username, password)
                    {
                        Operator = op,
                        DateAddedUTC = DateTime.UtcNow
                    });

                ctx.SaveChanges();

                return player;
            }
        }
Ejemplo n.º 4
0
        /// <summary>
        /// Store a setting
        /// </summary>
        /// <param name="key">Key.</param>
        /// <param name="value">Value.</param>
        public static void Set(string key, string value)
        {
            using (var ctx = new OTAContext())
            {
                var item = ctx.Settings.Where(x => x.DataKey == key).SingleOrDefault();
                if (item != null)
                {
                    item.DataValue = value;
                }
                else
                {
                    ctx.Settings.Add(item = new DataSetting()
                        {
                            DataKey = key,
                            DataValue = value
                        });
                }

                ctx.SaveChanges();
            }
        }
Ejemplo n.º 5
0
        /// <summary>
        /// Gets the first guest group from the database
        /// </summary>
        /// <returns>The guest group.</returns>
        public static Group GetGuestGroup()
        {
            if (!IsAvailable)
                throw new InvalidOperationException("No connector attached");

            using (var ctx = new OTAContext())
            {
                return ctx.Groups.Where(x => x.ApplyToGuests).FirstOrDefault();
            }
        }
 /// <summary>
 /// Gets the user password.
 /// </summary>
 /// <returns>The user password.</returns>
 /// <param name="username">Username.</param>
 public static string GetUserPassword(string username)
 {
     using (var ctx = new OTAContext())
     {
         return ctx.Players
             .Where(x => x.Name == username)
             .Select(x => x.Password)
             .FirstOrDefault();
     }
 }
Ejemplo n.º 7
0
 public static async Task<APIAccount> FindByName(string name)
 {
     using (var ctx = new OTAContext())
     {
         return await ctx.APIAccounts.FirstOrDefaultAsync(x => x.Username == name);
     }
 }
Ejemplo n.º 8
0
        /// <summary>
        /// Fetches the list of nodes for a user
        /// </summary>
        /// <returns>The nodes.</returns>
        /// <param name="username">Username.</param>
        public static NodePermission[] UserNodes(string username)
        {
            if (!IsAvailable)
                throw new InvalidOperationException("No connector attached");

            using (var ctx = new OTAContext())
            {
                return ctx.Players
                    .Where(p => p.Name == username)
                    .Join(ctx.PlayerNodes, pn => pn.Id, y => y.UserId, (a, b) => b)
                    .Join(ctx.Nodes, pn => pn.Id, nd => nd.Id, (a, b) => b)
                    .ToArray();
            }
        }
Ejemplo n.º 9
0
        /// <summary>
        /// Removes a player from a group
        /// </summary>
        /// <returns><c>true</c>, if user from group was removed, <c>false</c> otherwise.</returns>
        /// <param name="username">Username.</param>
        /// <param name="groupName">Group name.</param>
        public static bool RemoveUserFromGroup(string username, string groupName)
        {
            if (!IsAvailable)
                throw new InvalidOperationException("No connector attached");

            using (var ctx = new OTAContext())
            {
                var user = ctx.Players.Single(x => x.Name == username);
                var group = ctx.Groups.Single(x => x.Name == groupName);

                var range = ctx.PlayerGroups.RemoveRange(ctx.PlayerGroups.Where(x =>
                                    x.GroupId == group.Id &&
                                    x.UserId == user.Id
                                ));

                ctx.SaveChanges();

                return range.Any();
            }
        }
Ejemplo n.º 10
0
        /// <summary>
        /// Removes a node from a group
        /// </summary>
        /// <returns><c>true</c>, if group node was removed, <c>false</c> otherwise.</returns>
        /// <param name="groupName">Group name.</param>
        /// <param name="node">Node.</param>
        /// <param name="deny">If set to <c>true</c> deny.</param>
        public static bool RemoveGroupNode(string groupName, string node, Permission permission)
        {
            if (!IsAvailable)
                throw new InvalidOperationException("No connector attached");

            using (var ctx = new OTAContext())
            {
                var range = ctx.GroupNodes.RemoveRange(
                                from grp in ctx.Groups
                                               join nds in ctx.GroupNodes on grp.Id equals nds.GroupId
                                               select nds
                            );

                ctx.SaveChanges();

                return range.Any();
            }
        }
Ejemplo n.º 11
0
        /// <summary>
        /// Fetches the nodes for a group
        /// </summary>
        /// <returns>The nodes.</returns>
        /// <param name="groupName">Group name.</param>
        public static NodePermission[] GroupNodes(string groupName)
        {
            if (!IsAvailable)
                throw new InvalidOperationException("No connector attached");

            using (var ctx = new OTAContext())
            {
                return ctx.Groups
                    .Where(g => g.Name == groupName)
                    .Join(ctx.GroupNodes, grp => grp.Id, gn => gn.GroupId, (a, b) => b)
                    .Join(ctx.Nodes, gp => gp.Id, nd => nd.Id, (a, b) => b)
                    .ToArray();
            }
        }
 /// <summary>
 /// Finds a list of users matching a prefix
 /// </summary>
 /// <returns>The users by prefix.</returns>
 /// <param name="search">Search.</param>
 public static string[] FindUsersByPrefix(string search)
 {
     using (var ctx = new OTAContext())
     {
         if (OTAContext.IsSQLite)
         {
             var lowered = search.ToLower();
             return ctx.Players
                 .Where(x => x.Name.Length >= search.Length && x.Name.Substring(0, search.Length).ToLower() == lowered)
                 .Select(x => x.Name)
                 .ToArray();
         }
         else return ctx.Players
             .Where(x => x.Name.StartsWith(search))
             .Select(x => x.Name)
             .ToArray();
     }
 }
 /// <summary>
 /// Checks if a user exists
 /// </summary>
 /// <returns><c>true</c>, if exists was usered, <c>false</c> otherwise.</returns>
 /// <param name="username">Username.</param>
 public static bool UserExists(string username)
 {
     using (var ctx = new OTAContext())
     {
         return ctx.Players.Any(x => x.Name == username);
     }
 }
        /// <summary>
        /// Updates a user in the database.
        /// </summary>
        /// <returns><c>true</c>, if user was updated, <c>false</c> otherwise.</returns>
        /// <param name="username">Username.</param>
        /// <param name="password">Password.</param>
        /// <param name="op">If set to <c>true</c> op.</param>
        public static bool UpdateUser(string username, string password, bool? op = null)
        {
            if (username == null && op == null) throw new InvalidOperationException("You have not specified anything to be updated");
            using (var ctx = new OTAContext())
            {
                var player = ctx.Players.SingleOrDefault(p => p.Name == username);
                if (player == null) throw new InvalidOperationException("Cannot update a non-existent player");

                if (password != null) player.SetRawPassword(password);
                if (op.HasValue) player.Operator = op.Value;

                ctx.SaveChanges();

                return true;
            }
        }
Ejemplo n.º 15
0
        /// <summary>
        /// Fetches the lower most group for a player
        /// </summary>
        /// <remarks>There should always be one at this stage in OTA. The flexibility is just here.</remarks>
        /// <returns>The inherited group for user.</returns>
        /// <param name="username">Username.</param>
        public static Group GetInheritedGroupForUser(string username)
        {
            if (!IsAvailable)
                throw new InvalidOperationException("No connector attached");

            using (var ctx = new OTAContext())
            {
                return ctx.Players
                    .Where(x => x.Name == username)
                    .Join(ctx.PlayerGroups, pg => pg.Id, us => us.UserId, (a, b) => b)
                    .Join(ctx.Groups, pg => pg.GroupId, gr => gr.Id, (a, b) => b)
                    .FirstOrDefault();
            }
        }
Ejemplo n.º 16
0
        /// <summary>
        /// Adds a node to a group
        /// </summary>
        /// <returns><c>true</c>, if group node was added, <c>false</c> otherwise.</returns>
        /// <param name="groupName">Group name.</param>
        /// <param name="node">Node.</param>
        /// <param name="deny">If set to <c>true</c> deny.</param>
        public static bool AddGroupNode(string groupName, string node, Permission permission)
        {
            if (!IsAvailable)
                throw new InvalidOperationException("No connector attached");

            using (var ctx = new OTAContext())
            {
                var group = ctx.Groups.Where(x => x.Name == groupName).SingleOrDefault();
                var perm = FindOrCreateNode(node, permission);

                ctx.GroupNodes.Add(new GroupNode()
                    {
                        GroupId = group.Id,
                        NodeId = perm.Id
                    });

                ctx.SaveChanges();

                return true;
            }
        }
Ejemplo n.º 17
0
        /// <summary>
        /// Fetches the group names available
        /// </summary>
        /// <returns>The list.</returns>
        public static string[] GroupList()
        {
            if (!IsAvailable)
                throw new InvalidOperationException("No connector attached");

            using (var ctx = new OTAContext())
            {
                return ctx.Groups.Select(x => x.Name).ToArray();
            }
        }
Ejemplo n.º 18
0
        /// <summary>
        /// Adds a specific node to a user
        /// </summary>
        /// <returns><c>true</c>, if node to user was added, <c>false</c> otherwise.</returns>
        /// <param name="username">Username.</param>
        /// <param name="node">Node.</param>
        /// <param name="deny">If set to <c>true</c> deny.</param>
        public static bool AddNodeToUser(string username, string node, Permission permission)
        {
            if (!IsAvailable)
                throw new InvalidOperationException("No connector attached");

            using (var ctx = new OTAContext())
            {
                var user = ctx.Players.Single(x => x.Name == username);
                var perm = FindOrCreateNode(node, permission);

                var range = ctx.PlayerNodes.RemoveRange(ctx.PlayerNodes.Where(x =>
                                    x.NodeId == perm.Id &&
                                    x.UserId == user.Id
                                ));

                ctx.SaveChanges();

                return range.Any();
            }
        }
Ejemplo n.º 19
0
        /// <summary>
        /// Remove a group
        /// </summary>
        /// <returns><c>true</c>, if group was removed, <c>false</c> otherwise.</returns>
        /// <param name="name">Name.</param>
        public static bool RemoveGroup(string name)
        {
            if (!IsAvailable)
                throw new InvalidOperationException("No connector attached");

            using (var ctx = new OTAContext())
            {
                var range = ctx.Groups.RemoveRange(ctx.Groups.Where(x => x.Name == name));
                ctx.SaveChanges();

                return range.Any();
            }
        }
Ejemplo n.º 20
0
        /// <summary>
        /// Add or update a group
        /// </summary>
        /// <returns><c>true</c>, if the update group was added/updated, <c>false</c> otherwise.</returns>
        /// <param name="name">Name.</param>
        /// <param name="applyToGuests">If set to <c>true</c>, the group will be applied to guests.</param>
        /// <param name="parent">Parent.</param>
        /// <param name="r">The red chat component.</param>
        /// <param name="g">The green chat component.</param>
        /// <param name="b">The blue chat component.</param>
        /// <param name="prefix">Prefix.</param>
        /// <param name="suffix">Suffix.</param>
        public static Group AddOrUpdateGroup(string name, bool applyToGuests = false, string parent = null, byte r = 255, byte g = 255, byte b = 255, string prefix = null, string suffix = null)
        {
            if (!IsAvailable)
                throw new InvalidOperationException("No connector attached");

            using (var ctx = new OTAContext())
            {
                var group = ctx.Groups.SingleOrDefault(x => x.Name == name);
                if (group != null)
                {
                    group.ApplyToGuests = applyToGuests;
                    group.Parent = parent;
                    group.Chat_Red = r;
                    group.Chat_Green = g;
                    group.Chat_Blue = b;
                    group.Chat_Prefix = prefix;
                    group.Chat_Suffix = suffix;
                }
                else
                {
                    ctx.Groups.Add(group = new Group()
                        {
                            Name = name,
                            ApplyToGuests = applyToGuests,
                            Parent = parent,
                            Chat_Red = r,
                            Chat_Green = g,
                            Chat_Blue = b,
                            Chat_Prefix = prefix,
                            Chat_Suffix = suffix
                        });
                }

                ctx.SaveChanges();

                return group;
            }
        }
Ejemplo n.º 21
0
        /// <summary>
        /// Removes a specific node from a user
        /// </summary>
        /// <returns><c>true</c>, if node from user was removed, <c>false</c> otherwise.</returns>
        /// <param name="username">Username.</param>
        /// <param name="node">Node.</param>
        /// <param name="deny">If set to <c>true</c> deny.</param>
        public static bool RemoveNodeFromUser(string username, string node, Permission permission)
        {
            if (!IsAvailable)
                throw new InvalidOperationException("No connector attached");

            using (var ctx = new OTAContext())
            {
                var range = ctx.PlayerNodes.RemoveRange(
                                ctx.Players
                                    .Where(p => p.Name == username)
                                    .Join(ctx.PlayerNodes, x => x.Id, y => y.UserId, (a, b) => b)
                            );

                ctx.SaveChanges();

                return range.Any();
            }
        }
Ejemplo n.º 22
0
        /// <summary>
        /// Adds a user to a group
        /// </summary>
        /// <returns><c>true</c>, if user to group was added, <c>false</c> otherwise.</returns>
        /// <param name="username">Username.</param>
        /// <param name="groupName">Group name.</param>
        public static bool AddUserToGroup(string username, string groupName)
        {
            if (!IsAvailable)
                throw new InvalidOperationException("No connector attached");

            using (var ctx = new OTAContext())
            {
                var user = ctx.Players.Single(x => x.Name == username);
                var group = ctx.Groups.Single(x => x.Name == groupName);

                //Temporary until the need for more than one group
                if (ctx.PlayerGroups.Any(x => x.GroupId > 0))
                    throw new NotSupportedException("A player can only be associated to one group, please assign a parent to the desired group");

                ctx.PlayerGroups.Add(new PlayerGroup()
                    {
                        GroupId = group.Id,
                        UserId = user.Id
                    });

                ctx.SaveChanges();

                return true;
            }
        }
Ejemplo n.º 23
0
        /// <summary>
        /// Fetches the associated groups names for a user
        /// </summary>
        /// <returns>The group list.</returns>
        /// <param name="username">Username.</param>
        public static string[] UserGroupList(string username)
        {
            if (!IsAvailable)
                throw new InvalidOperationException("No connector attached");

            using (var ctx = new OTAContext())
            {
                return ctx.Players
                .Where(p => p.Name == username)
                    .Join(ctx.PlayerGroups, pg => pg.Id, y => y.UserId, (a, b) => b)
                    .Join(ctx.Groups, pg => pg.Id, g => g.Id, (a, b) => b)
                    .Select(x => x.Name)
                    .ToArray();
            }
        }
Ejemplo n.º 24
0
        /// <summary>
        /// Find a group by name
        /// </summary>
        /// <returns>The group.</returns>
        /// <param name="name">Name.</param>
        public static Group FindGroup(string name)
        {
            if (!IsAvailable)
                throw new InvalidOperationException("No connector attached");

            using (var ctx = new OTAContext()) return ctx.Groups.SingleOrDefault(x => x.Name == name);
        }
Ejemplo n.º 25
0
        private static Permission IsPermitted(string prmNode, bool prmIsGuest, string prmAuthentication = null)
        {
            if (!IsAvailable)
                throw new InvalidOperationException("No connector attached");

            var vPermissionValue = Permission.Denied;
            var vUserId = 0;
            var vGroupId = 0;
            var vPrevGroupId = 0;
            var vNodeFound = false;

            using (var ctx = new OTAContext())
            {
                if (prmIsGuest == false && prmAuthentication != null && prmAuthentication.Length > 0)
                {
                    var user = ctx.GetUser(prmAuthentication).Single();
                    if (user != null)
                    {
                        vUserId = user.Id;

                        if (user.Operator)
                            return  Permission.Permitted;
                    }

                    if (vUserId > 0)
                    {
                        /*
                        If the user has specific nodes then use them
                        If not then search for a group
                        If still none then try the guest permissions
                    */

                        /*Do we have any nodes?*/
                        var nodes = ctx.GetPermissionByNodeForUser(vUserId, prmNode).ToArray();
                        if (nodes != null && nodes.Length > 0)
                        {
                            if (nodes.Where(x => x.Permission == Permission.Denied).Count() == 0)
                            {
                                vPermissionValue = Permission.Permitted;
                                vNodeFound = true;
                            }
                            else
                            {
                                vPermissionValue = Permission.Denied;
                                vNodeFound = true;
                            }
                        }
                        else
                        {
                            /*
                            For each group, see if it has a permission
                            Else, if it has a parent recheck.
                            Else guestMode
                        */

                            var grp = ctx.GetUserGroups(vUserId).FirstOrDefault();
                            vGroupId = 0;
                            if (grp != null) vGroupId = grp.Id;
                            if (vGroupId > 0)
                            {
                                vPrevGroupId = vGroupId;
                                vNodeFound = false;

                                while (vGroupId > 0 && !vNodeFound)
                                {
                                    /* Check group permissions */

                                    var groupPermissions = ctx.GetPermissionByNodeForGroup(vGroupId, prmNode);

                                    if (groupPermissions.Where(x => x.Permission == Permission.Denied).Count() > 0)
                                    {
                                        vPermissionValue = Permission.Denied;
                                        vGroupId = 0;
                                        vNodeFound = true;
                                    }
                                    else if (groupPermissions.Where(x => x.Permission == Permission.Permitted).Count() > 0)
                                    {
                                        vPermissionValue = Permission.Permitted;
                                        vGroupId = 0;
                                        vNodeFound = true;
                                    }
                                    else
                                    {
                                        var par = ctx.GetParentForGroup(vGroupId).FirstOrDefault();
                                        if (par != null)
                                        {
                                            vGroupId = par.Id;
                                            if (vPrevGroupId == vGroupId)
                                            {
                                                vGroupId = 0;
                                            }

                                            vPrevGroupId = vGroupId;
                                        }
                                        else
                                        {
                                            vGroupId = 0;
                                        }
                                    }
                                }
                            }
                            if (!vNodeFound)
                            {
                                prmIsGuest = true;
                            }
                        }
                    }
                    else
                    {
                        /* Invalid user - try guest */
                        prmIsGuest = true;
                    }
                }

                if (!vNodeFound && prmIsGuest)
                {
                    if (ctx.GuestGroupHasNode(prmNode, Permission.Permitted))
                    {
                        vPermissionValue = Permission.Permitted;
                        vNodeFound = true;
                    }
                }

                return vPermissionValue;
            }
        }
Ejemplo n.º 26
0
        public static NodePermission FindOrCreateNode(string node, Permission permission)
        {
            if (!IsAvailable)
                throw new InvalidOperationException("No connector attached");

            using (var ctx = new OTAContext())
            {
                var existing = ctx.Nodes.SingleOrDefault(x => x.Node == node && x.Permission == permission);
                if (existing != null) return existing;
                else
                {
                    ctx.Nodes.Add(existing = new NodePermission()
                        {
                            Node = node,
                            Permission = permission
                        });

                    ctx.SaveChanges();

                    return existing;
                }
            }
        }
Ejemplo n.º 27
0
 public static async Task<APIAccountRole[]> GetRolesForAccount(int accountId)
 {
     using (var ctx = new OTAContext())
     {
         return await ctx.APIAccountsRoles.Where(x => x.AccountId == accountId).ToArrayAsync();
     }
 }
 /// <summary>
 /// Gets the user from the database by name
 /// </summary>
 /// <returns>The user.</returns>
 /// <param name="username">Username.</param>
 public static DbPlayer GetUser(string username)
 {
     using (var ctx = new OTAContext())
     {
         return ctx.Players
             .Where(x => x.Name == username)
             .FirstOrDefault();
     }
 }