Codiad / Codiad

Web Based, Cloud IDE
http://codiad.com
MIT License
2.85k stars 698 forks source link

Wordpress SSO kinda #634

Open ikantthink opened 10 years ago

ikantthink commented 10 years ago

@daeks asked me to share any solutions I came up with regarding wordpress. Maybe this will help you @waptug #627 (comment). My specific need was to provide a single project for each of my students. They only have access to their project. They login to wordpress click on a link and are directed to codiad, logged in, and their project is loaded. What I came up with is a terrible bit of hackery.... I added a function to hook the wordpress user login, set a cookie with the username then pick up that cookie within the codiad auth script. I was not terribly concerned with security, just making it work. I am not a PHP programmer by any stretch of the imagination... I repeated some default functions because I couldn't get them to work by other means.

Here is my WP function:

//set user cookie for codiad 
function set_user_cookie() {
    global $current_user;
    get_currentuserinfo();
    $theUser = $current_user->user_login;
    $av = bp_get_loggedin_user_avatar( 'html=false' );

    setcookie('d-user', $theUser , time()+3600*24);
    setcookie('d-avatar', $av , time()+3600*24);
}
add_action( 'init', 'set_user_cookie');

Here is my auth script: ("Jake" is the admin user who can jump between projects)

    /*
    *  Copyright (c) Codiad & Kent Safranski (codiad.com), distributed
    *  as-is and without warranty under the MIT License. See
    *  [root]/license.txt for more. This information must remain intact.
    */

    CustomAuth::login();

    class CustomAuth {

        private function getJSON($file) {

            $json = file_get_contents($file);
            $json = str_replace("|*/?>","",str_replace("";

            $write = fopen($file, 'wb') or die("can't open file ".$path.$file);
            fwrite($write, $data);

            fclose($write);
        }

        public function findUserData($user_data, $data_type, $data_value) {

            if(count($user_data) > 0)
            {
                foreach($user_data as $data) 
                {
                    if($data[$data_type] == $data_value)
                    {
                        return TRUE;
                    }
                }
            }

            return FALSE;
        }

        public static function login(){

            if (isset($_COOKIE['d-user'])) {

                $theUser = $_COOKIE["d-user"];
                $project_base_dir = "/var/www/public/code/";    
                $project_file_path = $project_base_dir . "data/projects.php";
                $user_file_path = $project_base_dir . "data/users.php";
                $user_dir_path = $project_base_dir . "workspace/";
                $projects = self::getJSON($project_file_path);

                if(!self::findUserData($projects, "name", $theUser)) 
                {   
                    $new_project = array (

                        'name' => $theUser,
                        'path' => $theUser
                    );

                    $projects[] = $new_project;

                    self::saveJSON($project_file_path, $projects);

                    $_SESSION['project'] = $theUser;
                }

                $users = self::getJSON($user_file_path);

                if(!self::findUserData($users, "username", $theUser)) 
                {   
                    $new_user = array (

                        'username' => $theUser,
                        'password' => $theUser,
                        'project' => $theUser
                    );

                    $users[] = $new_user;

                    self::saveJSON($user_file_path, $users);
                }

                $user_perm_file = $project_base_dir . 'data/' . $theUser . '_acl.php';

                if(!file_exists($user_perm_file)) 
                {
                    if ($theUser!='Jake') { 
                        self::saveJSON($user_perm_file, $theUser);
                    }
                }

                if (!file_exists($user_dir_path . $theUser)) {

                    mkdir($user_dir_path . $theUser , 0755, true);
                }

                $_SESSION['user'] = $theUser;
                $_SESSION['lang'] = 'en';
                $_SESSION['theme'] = "default";
                if ($theUser!='Jake') {
                    $_SESSION['project'] = $theUser;
                }

            } else {
                echo 'You must first login.';
                die;
            }                   

       }
   }

I also removed the logout from the right_bar.json and added a return link to WP:

    {
        "title": "Back to Wordpress",
        "admin": false,
        "icon": "icon-logout",
        "onclick": "window.location.assign('http://WP_URL')"
    }
waptug commented 10 years ago

Thank you so much for sharing. I will fiddle with this to see if I can get it to work.

I am working on a very similar use case in that I have a web apps user group that I am growing and I wanted to use WordPress to manage membership and general group postings and have all registered members get access to the codiad tool after they got registered on the wordpress site.

So the flow would work as such: user registers on the WordPress.org install and that then creates the access account on the Codiad user table. Would be nice if the code would also delete access from codiad if the member was removed from the WordPress.

http://www.MichaelScottMcGinn.Com Skype ID: waptug Cell: 1-206-909-0361

I build my business via referrals. If you know of some one who would benefit from my services I appreciate your trust in me and offer you this web site to register your referral to me. https://www.referralkey.com/geekzonebookscom

On Thu, Apr 17, 2014 at 9:00 AM, ikantthink notifications@github.comwrote:

@daeks https://github.com/daeks asked me to share any solutions I came up with regarding wordpress. Maybe this will help you @waptughttps://github.com/waptug #627 (comment)https://github.com/Codiad/Codiad/issues/627#issuecomment-40435131. My specific need was to provide a single project for each of my students. They only have access to their project. They login to wordpress click on a link and are directed to codiad, logged in, and their project is loaded. What I came up with is a terrible bit of hackery.... I added a function to hook the wordpress user login, set a cookie with the username then pick up that cookie within the codiad auth script. I was not terribly concerned with security, just making it work. I am not a PHP programmer by any stretch of the imagination... I repeated some default functions because I couldn't get them to work by other means.

Here is my WP function:

//set user cookie for codiad function set_user_cookie() { global $current_user; get_currentuserinfo(); $theUser = $current_user->user_login; $av = bp_get_loggedin_user_avatar( 'html=false' );

setcookie('d-user', $theUser , time()+3600*24);
setcookie('d-avatar', $av , time()+3600*24);

} add_action( 'init', 'set_user_cookie');

Here is my auth script:

/*
*  Copyright (c) Codiad & Kent Safranski (codiad.com), distributed
*  as-is and without warranty under the MIT License. See
*  [root]/license.txt for more. This information must remain intact.
*/

CustomAuth::login();

class CustomAuth {

    private function getJSON($file) {

        $json = file_get_contents($file);
        $json = str_replace("|*/?>","",str_replace("";

        $write = fopen($file, 'wb') or die("can't open file ".$path.$file);
        fwrite($write, $data);

        fclose($write);
    }

    public function findUserData($user_data, $data_type, $data_value) {

        if(count($user_data) > 0)
        {
            foreach($user_data as $data)
            {
                if($data[$data_type] == $data_value)
                {
                    return TRUE;
                }
            }
        }

        return FALSE;
    }

    public static function login(){

        if (isset($_COOKIE['d-user'])) {

            $theUser = $_COOKIE["d-user"];
            $project_base_dir = "/var/www/public/code/";
            $project_file_path = $project_base_dir . "data/projects.php";
            $user_file_path = $project_base_dir . "data/users.php";
            $user_dir_path = $project_base_dir . "workspace/";
            $projects = self::getJSON($project_file_path);

            if(!self::findUserData($projects, "name", $theUser))
            {
                $new_project = array (

                    'name' => $theUser,
                    'path' => $theUser
                );

                $projects[] = $new_project;

                self::saveJSON($project_file_path, $projects);

                $_SESSION['project'] = $theUser;
            }

            $users = self::getJSON($user_file_path);

            if(!self::findUserData($users, "username", $theUser))
            {
                $new_user = array (

                    'username' => $theUser,
                    'password' => $theUser,
                    'project' => $theUser
                );

                $users[] = $new_user;

                self::saveJSON($user_file_path, $users);
            }

            $user_perm_file = $project_base_dir . 'data/' . $theUser . '_acl.php';

            if(!file_exists($user_perm_file))
            {
                if ($theUser!='Jake' and $theUser!='Drwebb') {
                    self::saveJSON($user_perm_file, $theUser);
                }
            }

            if (!file_exists($user_dir_path . $theUser)) {

                mkdir($user_dir_path . $theUser , 0755, true);
            }

            $_SESSION['user'] = $theUser;
            $_SESSION['lang'] = 'en';
            $_SESSION['theme'] = "default";
            if ($theUser!='Jake') {
                $_SESSION['project'] = $theUser;
            }

        } else {
            echo 'You must first login <http://wp_login_url>.';
            die;
        }

   }

}

I also removed the logout from the right_bar.json and added a return link to WP:

{
    "title": "Back to Wordpress",
    "admin": false,
    "icon": "icon-logout",
    "onclick": "window.location.assign('http://WP_URL')"
}

— Reply to this email directly or view it on GitHubhttps://github.com/Codiad/Codiad/issues/634 .

ikantthink commented 10 years ago

@waptug You would probably best accomplish this by letting wordpress manage the user.php file. This is how I would do it if I rewrote this. You could grab the functions right out of codiad. It's mainly just array manipulation like I have done is this script. In wordpress you could hook new user creation, pass the details to a function where you copied the codiad code to handle user/project creation. Doing the same thing with the WP remove user hook, you would then only need to worry about project access per user. This presents the question of whether or not you want your users to be able to create projects or only have a single project space. You could use their acl file to manage this... Say you wanted to limit your users to 3 projects. If their .acl file contains only one or two then execute a project creation function, if it already has 3 then deny. Either way, you are just manipulating array in some php files in the DATA folder in codiad.

daeks commented 10 years ago

:+1: If it is working well, you/we can also add it to our wiki

ymdahi commented 10 years ago

+1 i would also love to see this plugin realized!

daeks commented 9 years ago

Issue is currently locked to prevent further spamming.