/// <summary>
        /// Update user information liek name,phone,role Version 2.0
        /// </summary>
        /// <param name="userId">UserId</param>
        /// <param name="userPhone">Phone Number</param>
        /// <param name="oldRoleId">Old Role Id</param>
        /// <param name="newRoleId">New Role Id</param>
        /// <param name="name">Name of User</param>
        /// <param name="boundaryId">BoundaryId</param>
        /// <param name="isEnable">Enable/Disabled</param>
        /// <returns></returns>
        public bool UpdateUser(string userId, string userPhone, string oldRoleId, string newRoleId, string name, int boundaryId, bool isEnable)
        {
            //ToDo :: Pending test
            AspNetUser   user         = GetUserWithRole(userId);
            AspNetRole   newRole      = GetRoles().SingleOrDefault(r => r.RoleId.Equals(newRoleId, StringComparison.OrdinalIgnoreCase));
            UserBoundary userBoundary = GetUserBoundary(userId, boundaryId);

            #region Validation
            if (user == null || newRole == null || userBoundary == null)
            {
                return(false);
            }
            #endregion

            try
            {
                _db.Database.BeginTransaction();
                user.PhoneNumber = userPhone;
                user.IsEnable    = isEnable;
                user.Name        = name;

                UpdateUserRole(userId, oldRoleId, user, newRole, userBoundary);

                _db.Entry(user).State = EntityState.Modified;
                _db.SaveChanges();
                _db.Database.CommitTransaction();
            }
            catch (Exception ex)
            {
                _db.Database.RollbackTransaction();
                LogHelper.WriteLog(TAG, ex);
                throw ex;
            }
            return(true);
        }
Example #2
0
        protected async void SignUp_ClickAsync(object sender, EventArgs e)
        {
            if (string.IsNullOrEmpty(txt_UserName.Value) || string.IsNullOrEmpty(txt_Avatar.Value) ||
                string.IsNullOrEmpty(txt_Role.Value) ||
                string.IsNullOrEmpty(txt_UserSmartspace.Value) ||
                string.IsNullOrEmpty(txt_Email.Value))
            {
                Msg_For_User("please fill all the details");
                return;
            }

            UserForm     userForm = GetUserFormFromComponent();
            UserBoundary user     = null;

            try
            {
                string requestURI = ConfigurationManager.ConnectionStrings[USER_CONTROLLER_SIGNUP].ToString();
                user = await _proxy.SignUp_Async(userForm, requestURI);
            }
            catch (Exception exception)
            {
                Console.WriteLine(exception.Message);
            }


            if (user.key == null)
            {
                Msg_For_User("Did NOT Signup- User might be already exist");
                return;
            }
            Msg_For_User("Signup Successfully");
            Response.Redirect("HomePage.aspx");
        }
        /// <summary>
        /// Get the UserBoundaries object of a user
        /// User should have unqiue record otherwise exception is thrown
        /// </summary>
        /// <param name="userId">UserId</param>
        /// <param name="boundaryId">BoundaryId</param>
        /// <returns>UserBoundaries object</returns>
        public UserBoundary GetUserBoundary(string userId, int boundaryId)
        {
            UserBoundary userBoundary = _db.UserBoundaries.Where(ub => ub.UserId.Equals(userId, StringComparison.OrdinalIgnoreCase) &&
                                                                 ub.BoundaryId == boundaryId &&
                                                                 ub.UserId != null).SingleOrDefault();

            return(userBoundary);
        }
Example #4
0
        public void GetUserBoundaries()
        {
            //Act
            UserBoundary boundary = repository.GetUserBoundary("54dac99f-a6d3-44f5-9e1f-8530e56d4567", 1);

            //Assert
            Assert.NotNull(boundary);
        }
Example #5
0
 protected void Page_Load(object sender, EventArgs e)
 {
     try
     {
         _user  = (UserBoundary)Session["User_Logged"];
         _proxy = new Proxy();
         onloadAsync();
         if (_user.role.Equals("MANAGER"))
         {
             txt_Options.Visible = false;
         }
     }
     catch (Exception exception)
     {
         Console.WriteLine(exception);
     }
 }
        /// <summary>
        /// Delete the [Boundary] of user and [Role] associated with that boundary,
        /// if successfull then return true
        /// </summary>
        /// <param name="username">SubjectId/UserId</param>
        /// <param name="boundaryId">Targeted boundary id</param>
        /// <returns>true if success</returns>
        public bool DeleteUserBoundary(string userId, int boundaryId)
        {
            UserBoundary userBoundary = (from ub in _db.UserBoundaries
                                         where ub.BoundaryId == boundaryId &&
                                         ub.UserId.Equals(userId, StringComparison.OrdinalIgnoreCase)
                                         select ub).Include(ub => ub.AspNetUserRoles)
                                        .SingleOrDefault();

            if (userBoundary != null)
            {
                _db.UserBoundaries.Remove(userBoundary);
                _db.SaveChanges();
                return(true);
            }

            return(false);
        }
Example #7
0
        protected async void LoginButtonAsync(object sender, EventArgs e)
        {
            if (string.IsNullOrEmpty(txt_UserEmail.Text))
            {
                txt_UserEmail.Text = ConfigurationManager.ConnectionStrings[USER_EMAIL].ToString();
            }
            if (string.IsNullOrEmpty(txt_Smartspace.Text))
            {
                txt_Smartspace.Text = ConfigurationManager.ConnectionStrings[USERSMARTSPACE].ToString();
            }
            string UserEmail  = txt_UserEmail.Text;
            string SmartSpace = txt_Smartspace.Text;

            if (string.IsNullOrEmpty(UserEmail) || string.IsNullOrEmpty(SmartSpace))
            {
                Msg_For_User("Please insert UserBoundary Email and SmartSpace");
                return;
            }
            string URLAdress = ConfigurationManager.ConnectionStrings[LOGIN_CONNECTION_STRING].ToString();

            _user = await _proxy.Login(UserEmail, SmartSpace, URLAdress);

            if (_user.key == null)
            {
                Msg_For_User("User is not Exist");
                return;
            }

            if (_user.role.Equals("ADMIN") || (!_user.role.Equals("PLAYER") && !_user.role.Equals("MANAGER")))
            {
                Msg_For_User("ONLY MANAGER and PLAYER are allowed");
                return;
            }

            Msg_For_User("Succeed");
            Session["User_Logged"] = _user;
            Response.Redirect("BookStore.aspx", false);
        }
Example #8
0
        public async Task <UserBoundary> SignUp_Async(UserForm user, string URLAdress)
        {
            using (var client = new HttpClient())
            {
                UserBoundary userBoundary = null;
                var          response     = await client.PostAsync(URLAdress,
                                                                   new StringContent(Newtonsoft.Json.JsonConvert.SerializeObject(user), Encoding.UTF8,
                                                                                     "application/json"));

                string responseBody = await response.Content.ReadAsStringAsync();

                try
                {
                    userBoundary = new JavaScriptSerializer().Deserialize <UserBoundary>(responseBody);
                }
                catch (Exception e)
                {
                    Console.Write(e.Message);
                }

                return(userBoundary);
            }
        }
Example #9
0
        public async Task <UserBoundary> Login_Async(string UserEmail, string SmartSpace, string URLAdress)
        {
            using (var client = new HttpClient())
            {
                string login_connection_string =
                    URLAdress + "/" + SmartSpace + "/" + UserEmail;
                HttpResponseMessage response = await client.GetAsync(login_connection_string);

                string responseBody = await response.Content.ReadAsStringAsync();

                UserBoundary user = null;
                try
                {
                    user = //new UserBoundary(responseBody);
                           new JavaScriptSerializer().Deserialize <UserBoundary>(responseBody);
                }
                catch (Exception e)
                {
                    Console.Write(e.Message);
                }

                return(user);
            }
        }
Example #10
0
 protected void Page_Load(object sender, EventArgs e)
 {
     _element              = (ElementBoundary)Session["The_Chosen_Book"];
     _user                 = (UserBoundary)Session["User_Logged"];
     _proxy                = new Proxy();
     txt_BookID.Value      = _element.key.id;
     txt_BookCreator.Value = _element.creator.ToString();
     txt_BookName.Value    = _element.name;
     if (_element.elementProperties != null)
     {
         if (_element.elementProperties.ContainsKey("Genre"))
         {
             txt_Genre.Value = _element.elementProperties["Genre"].ToString();
         }
         if (_element.elementProperties.ContainsKey("pagesAmount"))
         {
             txt_Amount_of_pages.Value = _element.elementProperties["pagesAmount"].ToString();
         }
         if (_element.elementProperties.ContainsKey("Price"))
         {
             txt_Price.Value = _element.elementProperties["Price"].ToString();
         }
         if (_element.elementProperties.ContainsKey("Language"))
         {
             txt_Language.Value = _element.elementProperties["Language"].ToString();
         }
         if (_element.elementProperties.ContainsKey("Publisher"))
         {
             txt_Publisher.Value = _element.elementProperties["Publisher"].ToString();
         }
         if (_element.elementProperties.ContainsKey("Author"))
         {
             txt_Author.Value = _element.elementProperties["Author"].ToString();
         }
     }
 }
Example #11
0
        public async Task <UserBoundary> Login(string UserEmail, string SmartSpace, string IPAdress)
        {
            UserBoundary user = await Login_Async(UserEmail, SmartSpace, IPAdress);

            return(user);
        }
        /// <summary>
        /// Update User role information,if new and old roleid are same then do nothing
        /// </summary>
        /// <param name="userId"></param>
        /// <param name="oldRoleId"></param>
        /// <param name="user"></param>
        /// <param name="newRole"></param>
        /// <param name="userBoundary"></param>
        private static void UpdateUserRole(string userId, string oldRoleId, AspNetUser user, AspNetRole newRole, UserBoundary userBoundary)
        {
            if (oldRoleId.Equals(newRole.RoleId, StringComparison.OrdinalIgnoreCase))
            {
                return;
            }
            //remove the role first
            AspNetUserRole userAssignedRole = user.AspNetUserRoles.SingleOrDefault(ur => ur.RoleId.Equals(oldRoleId, StringComparison.OrdinalIgnoreCase) &&
                                                                                   ur.UserBoundaryId == userBoundary.UserBoundaryId &&
                                                                                   ur.UserId.Equals(userId, StringComparison.OrdinalIgnoreCase));

            if (userAssignedRole != null)
            //remove the role assigned to user
            {
                user.AspNetUserRoles.Remove(userAssignedRole);
            }


            //Assign new role
            user.AspNetUserRoles.Add(new AspNetUserRole
            {
                UserId         = user.UserId,
                User           = user,
                RoleId         = newRole.RoleId,
                Role           = newRole,
                UserBoundaryId = userBoundary.UserBoundaryId,
                UserBoundary   = userBoundary
            });
        }
        /// <summary>
        /// Create New User, return true if user is successfully created
        /// </summary>
        /// <param name="userName"></param>
        /// <param name="name"></param>
        /// <param name="phoneNumber"></param>
        /// <param name="roleId">Role assigned to user</param>
        /// <param name="boundaryId">Boundary Id</param>
        /// <param name="isEnable"></param>
        /// <returns>bool</returns>
        public bool CreateUser(string userName, string name, string phoneNumber, string roleId, int boundaryId, bool isEnable)
        {
            UserBoundary ub       = new UserBoundary();
            Boundary     boundary = GetBoundaries().SingleOrDefault(b => b.BoundaryId == boundaryId);
            AspNetRole   role     = GetRoles().SingleOrDefault(r => r.RoleId.Equals(roleId, StringComparison.OrdinalIgnoreCase));

            #region Boundary/Role Validation
            if (boundary == null || role == null)
            {
                return(false);
            }
            #endregion


            try
            {
                AspNetUser user = new AspNetUser()
                {
                    UserId       = Guid.NewGuid().ToString(),
                    UserName     = userName,
                    Name         = name,
                    Email        = userName,
                    Password     = "******" + Guid.NewGuid(),
                    PhoneNumber  = phoneNumber,
                    IsEnable     = isEnable,
                    CreatedDate  = DateTime.Now,
                    ModifiedDate = DateTime.Now,
                    ExpiryDate   = DateTime.Now,
                    StatusId     = StatusConstants.Active
                };

                #region User boundaries
                ub.Boundary         = boundary;
                ub.BoundaryId       = boundaryId;
                ub.UserId           = user.UserId;
                user.UserBoundaries = new List <UserBoundary> {
                    ub
                };
                #endregion

                #region User Claims
                List <AspNetUserClaim> userClaims = new List <AspNetUserClaim>()
                {
                    new AspNetUserClaim()
                    {
                        User       = user,
                        UserId     = user.UserId,
                        ClaimType  = "Subject",
                        ClaimValue = user.UserId
                    },
                    new AspNetUserClaim()
                    {
                        User       = user,
                        UserId     = user.UserId,
                        ClaimType  = "Name",
                        ClaimValue = user.Name
                    },
                    new AspNetUserClaim()
                    {
                        User       = user,
                        UserId     = user.UserId,
                        ClaimType  = "Email",
                        ClaimValue = user.Email
                    },
                    new AspNetUserClaim()
                    {
                        User       = user,
                        UserId     = user.UserId,
                        ClaimType  = "PhoneNumber",
                        ClaimValue = user.PhoneNumber
                    },
                    new AspNetUserClaim()
                    {
                        User       = user,
                        UserId     = user.UserId,
                        ClaimType  = "Role",
                        ClaimValue = role.RoleId
                    },
                    new AspNetUserClaim()
                    {
                        User       = user,
                        UserId     = user.UserId,
                        ClaimType  = "Guest",
                        ClaimValue = "Guest"
                    },
                    new AspNetUserClaim()
                    {
                        User       = user,
                        UserId     = user.UserId,
                        ClaimType  = "Installer",
                        ClaimValue = "Installer"
                    }
                };
                user.UserClaims = userClaims;
                #endregion

                using (_db.Database.BeginTransaction())
                {
                    user.AspNetUserRoles = new List <AspNetUserRole>();
                    user.AspNetUserRoles.Add(new AspNetUserRole()
                    {
                        Role = role, RoleId = role.RoleId, UserBoundary = ub, UserBoundaryId = ub.UserBoundaryId
                    });

                    _db.AspNetUsers.Add(user);
                    _db.SaveChanges();
                    _db.Database.CommitTransaction();
                    return(true);
                }
            }
            catch (Exception ex)
            {
                _db.Database.RollbackTransaction();
                LogHelper.WriteLog(TAG, ex);
                throw ex;
            }
        }