Ejemplo n.º 1
0
        public Result <OlescUser> Register(OlescUser newUser, UserProfile newUserProfile, List <OlescRole> roles)
        {
            int NumOfRecords = 0;

            /* Create the result object ; which will be returned at the end
             * of the method */
            Result <OlescUser> result = new Result <OlescUser>();
            /* Database Connection */
            OlescEntities dbCon = null;

            try
            {
                /* Database Connection */
                dbCon = new OlescEntities();

                /* Adding a newUser to User table,
                 * and returns the object with user ID */
                newUser.isActive = "A";

                /* Take the user password and turn in into MD5 hash,
                 * then save the hash in the database */
                newUser.Password = StringUtility.CreateMD5Hash(newUser.Password);

                newUser = dbCon.OlescUsers.Add(newUser);

                /* assign ID to UserProfile table; because it's PF */
                //newUserProfile.UserID = newUser.ID;
                newUserProfile.user = newUser;

                /* Adding a newUserProfile to UserProfile table,
                 * and returns the object */
                newUserProfile = dbCon.UserProfiles.Add(newUserProfile);

                foreach (OlescRole role in roles)
                {
                    /* creating an object for userrole, using userrole table */
                    OlescUserRole newUserRole = new OlescUserRole();

                    /* What I need to assign a role is to have RoleID and UserID
                     * "Check database table UserRole"*/
                    //newUserRole.UserID = newUser.ID;
                    newUserRole.User   = newUser;
                    newUserRole.RoleID = role.ID;

                    /*Adding the role assigned tp userRole table*/
                    newUserRole = dbCon.OlescUserRoles.Add(newUserRole);
                }

                NumOfRecords = dbCon.SaveChanges();
                /* if there is at least 1 record saved -> true, if not -> false */
                if (NumOfRecords >= 1)
                {
                    result.isSuccess = true;
                    result.message   = "Thank you " + newUser.UserProfile.FirstName + " for regstering in OLESC!";


                    NotificationManagementService nms = new NotificationManagementService();

                    /* Email notification service */
                    /* Conent */
                    String emailContent;
                    emailContent  = "Thank you for registering in OLESC";
                    emailContent += "\nYour Username is your email: " + newUser.Email;
                    emailContent += "\nEnjoy our Online Learning Srvices!";
                    emailContent += "\n\nRegards,\nOLESC Team";

                    /* Subject*/
                    String emailSubject;
                    emailSubject = "OLESC Registration";

                    /* Sending the email */
                    nms.Notify(newUser.Email, null, null, emailSubject, emailContent);
                }
                else
                {
                    result.isSuccess = false;
                    result.message   = "Error in registration, please check you information";
                }
            }
            catch (Exception e)
            {
                result.isSuccess    = false;
                result.message      = e.Message;
                result.resultObject = null;
            }
            /* Whatever you want to clean up, close after error to delete from memory*/
            finally
            {
                if (dbCon != null)
                {
                    /* Same as in Java dbCon.Close() */
                    dbCon.Dispose();
                }
            }

            return(result);
        }
Ejemplo n.º 2
0
        public Result <OlescUser> ChangePassword(string username, string password)
        {
            int NumOfRecords = 0;

            /* Create the result object ; which will be returned at the end
             * of the method */
            Result <OlescUser> result = new Result <OlescUser>();
            /* Database Connection */
            OlescEntities dbCon = null;

            try
            {
                /* Database Connection */
                dbCon = new OlescEntities();

                OlescUser user = dbCon.OlescUsers.Where(u => u.Email.Equals(username)).FirstOrDefault();
                if (user != null)
                {
                    password = StringUtility.CreateMD5Hash(password);

                    if (!user.Password.Equals(password))
                    {
                        user.Password = password;
                        NumOfRecords  = dbCon.SaveChanges();
                        /* if there is at least 1 record saved -> true, if not -> false */
                        if (NumOfRecords >= 1)
                        {
                            result.isSuccess = true;
                            result.message   = "Password changed successfully";

                            NotificationManagementService nms = new NotificationManagementService();

                            /* Email notification service */
                            /* Conent */
                            String emailContent;
                            emailContent  = "Dear " + user.UserProfile.FirstName;
                            emailContent += "\nThis email is notify you that your password has been changed.";
                            emailContent += "\nIf you haven't changed your password, please reset your password in the login page.";
                            emailContent += "\n\nRegards,\nOLESC Team";

                            /* Subject*/
                            String emailSubject;
                            emailSubject = "Your Password has been Changed";

                            /* Sending the email */
                            nms.Notify(user.Email, null, null, emailSubject, emailContent);
                        }
                        else
                        {
                            result.isSuccess = false;
                            result.message   = "Error changing password";
                        }
                    }
                    else
                    {
                        result.isSuccess = false;
                        result.message   = "Old password is the same as the new password";
                    }
                }
                else
                {
                    result.isSuccess = false;
                    result.message   = "User does not exist";
                }
            }
            catch (Exception e)
            {
                result.isSuccess    = false;
                result.message      = e.Message;
                result.resultObject = null;
            }
            finally
            {
                if (dbCon != null)
                {
                    /* Same as in Java dbCon.Close() */
                    dbCon.Dispose();
                }
            }

            return(result);
        }