示例#1
0
        public override MembershipUser GetUser(object providerUserKey, bool userIsOnline)
        {
            N2.Security.Items.UserList userContainer = Bridge.GetUserContainer(false);
            if (userContainer == null)
            {
                return(null);
            }

            int _userId = 0;

            if (providerUserKey is int)
            {
                _userId = (int)providerUserKey;
            }
            else if (providerUserKey is Guid)
            {
                /// http://forums.asp.net/t/1266765.aspx
                Guid guid = (Guid)providerUserKey;
                //extract an integer from the beginning of the Guid
                byte[] _bytes = guid.ToByteArray();
                _userId = ((int)_bytes[0]) | ((int)_bytes[1] << 8) | ((int)_bytes[2] << 16) | ((int)_bytes[3] << 24);
            }

            var users = Bridge.Repository.Find(Parameter.Equal("ID", _userId)
                                               & Parameter.TypeEqual(typeof(User).Name)
                                               & Parameter.Equal("Parent", userContainer)).OfType <User>();

            return(users.Select(u => u.GetMembershipUser(Name)).FirstOrDefault());
        }
示例#2
0
        public override MembershipUserCollection FindUsersByEmail(string emailToMatch, int pageIndex, int pageSize, out int totalRecords)
        {
            MembershipUserCollection muc = new MembershipUserCollection();

            Items.UserList userContainer = Bridge.GetUserContainer(false);
            if (userContainer == null)
            {
                totalRecords = 0;
                return(muc);
            }
            IList <ContentItem> users = Bridge.Finder
                                        .Where.Detail("Email").Eq(emailToMatch)
                                        .And.Type.Eq(typeof(Items.User))
                                        .And.Parent.Eq(userContainer)
                                        .Select();

            totalRecords = users.Count;
            Collections.CountFilter.Filter(users, pageIndex * pageSize, pageSize);

            foreach (Items.User u in users)
            {
                muc.Add(u.GetMembershipUser(Name));
            }
            return(muc);
        }
 public override string[] GetAllRoles()
 {
     Items.UserList users = Bridge.GetUserContainer(false);
     if (users == null)
     {
         return(Bridge.DefaultRoles);
     }
     return(users.GetRoleNames());
 }
示例#4
0
 public int GetUsersCount()
 {
     Items.UserList users = GetUserContainer(false);
     if (users == null)
     {
         return(0);
     }
     return(users.Children.OfType <User>().Count());
 }
示例#5
0
 public override MembershipUserCollection GetAllUsers(int pageIndex, int pageSize, out int totalRecords)
 {
     N2.Security.Items.UserList users = Bridge.GetUserContainer(false);
     if (users == null)
     {
         totalRecords = 0;
         return(new MembershipUserCollection());
     }
     return(users.GetMembershipUsers(Name, pageIndex * pageSize, pageSize, out totalRecords));
 }
示例#6
0
        public virtual IList <Items.User> GetUsers(string username, int firstResult, int maxResults)
        {
            Items.UserList users = GetUserContainer(false);
            if (users == null)
            {
                return(new List <Items.User>());
            }

            return(Repository.Find((Parameter.Equal("Parent", users) & Parameter.TypeEqual(userType.Name) & Parameter.Like("Name", username)).Skip(firstResult).Take(maxResults))
                   .OfType <User>().ToList());
        }
        public override bool DeleteRole(string roleName, bool throwOnPopulatedRole)
        {
            if (throwOnPopulatedRole && GetUsersInRole(roleName).Length > 0)
            {
                throw new N2Exception("Role {0} cannot be deleted since it has users attached to it.", roleName);
            }

            Items.UserList ul = Bridge.GetUserContainer(true);
            ul.RemoveRole(roleName);
            Bridge.Save(ul);
            return(true);
        }
示例#8
0
        /// <summary> Returns users (UserNames) in specified role </summary>
        public virtual string[] GetUsersInRole(string roleName, int maxResults)
        {
            Items.UserList users = GetUserContainer(false);
            if (users == null)
            {
                return new string[] { }
            }
            ;

            return(Repository.Find((Parameter.Equal("Parent", users) & Parameter.TypeEqual(userType.Name) & Parameter.Equal("Roles", roleName).Detail(true)).Take(maxResults))
                   .OfType <User>().Select(u => u.Name).ToArray());
        }
        public override string GetUserNameByEmail(string email)
        {
            N2.Security.Items.UserList userContainer = Bridge.GetUserContainer(false);
            if (userContainer == null)
            {
                return(null);
            }
            var userNames = Bridge.Repository.Select(Parameter.Equal("Email", email) & Parameter.TypeEqual(typeof(User).Name) & Parameter.Equal("Parent", userContainer),
                                                     "Name").Select(d => d["Name"]);

            return(userNames.OfType <string>().FirstOrDefault());
        }
示例#10
0
        protected Items.UserList CreateUserContainer(ContentItem parent)
        {
            Items.UserList m = activator.CreateInstance <Items.UserList>(parent);
            m.Title = "Users";
            m.Name  = UserContainerName;
            foreach (string role in DefaultRoles)
            {
                m.AddRole(role);
            }

            Save(m);
            return(m);
        }
示例#11
0
        public override int GetNumberOfUsersOnline()
        {
            N2.Security.Items.UserList users = Bridge.GetUserContainer(false);
            if (users == null)
            {
                return(0);
            }

            int userIsOnlineTimeWindow = (Membership.UserIsOnlineTimeWindow > 0 ? Membership.UserIsOnlineTimeWindow : 20);

            return((int)Bridge.Repository.Count(Parameter.Equal("Parent", users)
                                                & Parameter.GreaterOrEqual("LastActivityDate", N2.Utility.CurrentTime().AddMinutes(-userIsOnlineTimeWindow)).Detail()));
        }
示例#12
0
        public virtual IList <Items.User> GetUsers(string username, int firstResult, int maxResults)
        {
            Items.UserList users = GetUserContainer(false);
            if (users == null)
            {
                return(new List <Items.User>());
            }

            return(finder.Where.Parent.Eq(users)
                   .And.Type.Eq(userType)
                   .And.Name.Like(username)
                   .FirstResult(firstResult)
                   .MaxResults(maxResults)
                   .Select <Items.User>());
        }
示例#13
0
        /// <summary> Returns list of users, identified by arguments </summary>
        /// <param name="username"> Username sql like expression </param>
        /// <param name="firstResult"> Number of users to be skipped </param>
        /// <param name="maxResults"> Max users to be returned </param>
        public virtual IList <Items.User> GetUsers(string username, int firstResult, int maxResults)
        {
            Items.UserList users = GetUserContainer(false);
            if (users == null)
            {
                return(new List <Items.User>());
            }

            // TODO: consider know weakness - SQL like operator evaluates wild characters, e.g. underscore
            //       what may select additional unwanted users!
            var query = Parameter.Equal("Parent", users) & Parameter.TypeEqual(userType.Name) & Parameter.Like("Name", username);

            query = query.Skip(firstResult).Take(maxResults);
            return(Repository.Find(query).OfType <User>().ToList());
        }
示例#14
0
        public override int GetNumberOfUsersOnline()
        {
            Items.UserList users = Bridge.GetUserContainer(false);
            if (users == null)
            {
                return(0);
            }
            int online = 0;

            foreach (Items.User u in users.Children)
            {
                if (u.LastActivityDate < DateTime.Now.AddMinutes(-20))
                {
                    online++;
                }
            }
            return(online);
        }
示例#15
0
        public override string GetUserNameByEmail(string email)
        {
            Items.UserList userContainer = Bridge.GetUserContainer(false);
            if (userContainer == null)
            {
                return(null);
            }
            IList <ContentItem> users = Bridge.Finder
                                        .Where.Detail("Email").Eq(email)
                                        .And.Type.Eq(typeof(Items.User))
                                        .And.Parent.Eq(userContainer)
                                        .Select();

            foreach (Items.User u in users)
            {
                return(u.Name);
            }
            return(null);
        }
示例#16
0
        /// <summary> Returns list of users, identified by arguments </summary>
        /// <param name="username"> Username sql like expression </param>
        /// <param name="firstResult"> Number of users to be skipped </param>
        /// <param name="maxResults"> Max users to be returned </param>
        public virtual IList <Items.User> GetUsers(string username, int firstResult, int maxResults)
        {
            Items.UserList users = GetUserContainer(false);
            if (users == null)
            {
                return(new List <Items.User>());
            }

            var query =
                Parameter.Equal("Parent", users)
                & Parameter.TypeEqual(userType.Name);

            var cachedResults = Repository.Find(query).OfType <User>().ToList();

            // TODO: Eventually move this filter to the Parameter above (e.g. &Parameter.Like("Name", username); ) once Parameter supports Like filters properly.
            // TODO: Also do paging in the query, e.g.  `query = query.Skip(firstResult).Take(maxResults);`
            return((from user in cachedResults
                    where user.Name.Contains(username)
                    select user).Skip(firstResult).Take(maxResults).ToList());
        }
示例#17
0
        public override MembershipUser GetUser(object providerUserKey, bool userIsOnline)
        {
            Items.UserList userContainer = Bridge.GetUserContainer(false);
            if (userContainer == null)
            {
                return(null);
            }

            int _userId = 0;

            if (providerUserKey is int)
            {
                _userId = (int)providerUserKey;
            }
            else if (providerUserKey is Guid)
            {
                /// http://forums.asp.net/t/1266765.aspx
                Guid guid = (Guid)providerUserKey;
                //extract an integer from the beginning of the Guid
                byte[] _bytes = guid.ToByteArray();
                _userId = ((int)_bytes[0]) | ((int)_bytes[1] << 8) | ((int)_bytes[2] << 16) | ((int)_bytes[3] << 24);
            }

            IList <ContentItem> users = Bridge.Finder
                                        .Where.ID.Eq(_userId)
                                        .And.Type.Eq(typeof(Items.User))
                                        .And.Parent.Eq(userContainer)
                                        .Select();

            foreach (Items.User u in users)
            {
                if (userIsOnline)
                {
                    u.LastLoginDate = DateTime.Now;
                }
                return(u.GetMembershipUser(Name));
            }
            return(null);
        }
        public override MembershipUser GetUser(object providerUserKey, bool userIsOnline)
        {
            N2.Security.Items.UserList userContainer = Bridge.GetUserContainer(false);
            if (userContainer == null)
            {
                return(null);
            }

            int _userId = 0;

            if (providerUserKey is int)
            {
                _userId = (int)providerUserKey;
            }
            else if (providerUserKey is Guid)
            {
                /// http://forums.asp.net/t/1266765.aspx
                Guid guid = (Guid)providerUserKey;
                //extract an integer from the beginning of the Guid
                byte[] _bytes = guid.ToByteArray();
                _userId = ((int)_bytes[0]) | ((int)_bytes[1] << 8) | ((int)_bytes[2] << 16) | ((int)_bytes[3] << 24);
            }

            var users = Bridge.Repository.Find(Parameter.Equal("ID", _userId)
                                               & Parameter.Equal("Parent", userContainer)
                                               // & Parameter.TypeEqual(typeof(User).Name)  // review (JH): this might be a problem -
                                               //                 ItemBridge supports User or any type extended from User!
                                               // Solution:    remove the term -
                                               //              it does not contribute to query effectiveness due to low selectivity,
                                               //              and does not contribute to logics.
                                               // Note: Linq method OfType returns object that can be casted to specific type,
                                               //       on the ohter hand Parameter.TypeEqual returns object of exactly the specified type.
                                               //       http://msdn.microsoft.com/en-us/library/vstudio/bb360913(v=vs.100).aspx
                                               )
                        .OfType <User>();

            return(users.Select(u => u.GetMembershipUser(Name)).FirstOrDefault());
        }
示例#19
0
        public override MembershipUserCollection FindUsersByEmail(string emailToMatch, int pageIndex, int pageSize, out int totalRecords)
        {
            MembershipUserCollection muc = new MembershipUserCollection();

            N2.Security.Items.UserList userContainer = Bridge.GetUserContainer(false);
            if (userContainer == null)
            {
                totalRecords = 0;
                return(muc);
            }
            IList <ContentItem> users = Bridge.Repository.Find(Parameter.Equal("Email", emailToMatch)
                                                               & Parameter.TypeEqual(typeof(N2.Security.Items.User).Name)
                                                               & Parameter.Equal("Parent", userContainer)).ToList();

            totalRecords = users.Count;
            N2.Collections.CountFilter.Filter(users, pageIndex * pageSize, pageSize);

            foreach (N2.Security.Items.User u in users)
            {
                muc.Add(u.GetMembershipUser(Name));
            }
            return(muc);
        }
示例#20
0
        /// <summary>Returns user identified by email, null: not found </summary>
        public virtual Items.User FindUserByEmail(string email)
        {
            try
            {
                if (string.IsNullOrEmpty(email))
                {
                    return(null);
                }

                Items.UserList users = GetUserContainer(false);
                if (users == null)
                {
                    return(null);
                }

                // Note: email case sensitivity is defined by underlying storege settings
                var usersByEmail = Repository.Find((Parameter.Equal("Parent", users) & Parameter.Equal("Email", email).Detail(true))).OfType <User>().ToList();
                if (usersByEmail.Count == 0)
                {
                    return(null);
                }
                else if (usersByEmail.Count == 1)
                {
                    return(usersByEmail[0]);
                }
                else /* multiple users found, the first one returned */
                {
                    return(usersByEmail[0]);
                }
            }
            catch (Exception ex)
            {
                logger.Error(ex);
                return(null);
            }
        }
示例#21
0
 public override void CreateRole(string roleName)
 {
     Items.UserList ul = Bridge.GetUserContainer(true);
     ul.AddRole(roleName);
     Bridge.Save(ul);
 }