How may i let every user add sites?


(xsir317) #1

style_emoticons/<#EMO_DIR#>/blink.gif

you know,in piwik ,only super user can add sites.

now i want others could add their own sites,how could i do?

i was readding the source code,at
function addSite
there is a check, Piwik::checkUserIsSuperUser();

i removed this check, but it seems stupid: the common user added a site,but they can not access it.

i’m poor at english, do you understand me ? i mean even if i remove the super-user check,the common user still can not add a site and manage it.

i hope who could help me…


(vipsoft) #2

You might consider creating a plugin or adding a form that:

  1. provides a means for a user to enter a site to be added
  2. on the backend, uses the API to add the site (using the superuser auth token)
  3. similarly, uses the API to make the user an admin of the above site

This would be cleaner than hacking the permissions code, and less likely to break in a Piwik upgrade.


(xsir317) #3

[quote=vipsoft @ Apr 5 2009, 01:11 AM]You might consider creating a plugin or adding a form that:

  1. provides a means for a user to enter a site to be added
  2. on the backend, uses the API to add the site (using the superuser auth token)
  3. similarly, uses the API to make the user an admin of the above site

This would be cleaner than hacking the permissions code, and less likely to break in a Piwik upgrade.[/quote]
Thanks a lot! i have done it!


(kolchak) #4

Care to share your code style_emoticons/<#EMO_DIR#>/smile.gif


(CreativeNotice) #5

Yes please do share your code. I’m very interested in this feature as well. Actually very surprised it has not been included in the core.


(xsir317) #6

hmm…

static public function addSite( $siteName, $urls )
{
	//Piwik::checkUserIsSuperUser();
	
	self::checkName($siteName);
	$urls = self::cleanParameterUrls($urls);
	self::checkUrls($urls);
	self::checkAtLeastOneUrl($urls);
	
	$db = Zend_Registry::get('db');
	
	$url = $urls[0];
	$urls = array_slice($urls, 1);
	
	$db->insert(Piwik::prefixTable("site"), array(
								'name' => $siteName,
								'main_url' => $url,
								)
							);
								
	$idSite = $db->lastInsertId();
	$userLogin = Piwik::getCurrentUserLogin();
            //cannot use setUserAccess to set access
	//Piwik_UsersManager_API::setUserAccess($userLogin,'admin',$idSite);
	try {
		Piwik::checkUserIsSuperUser();
	}
	catch (Exception $e)
	{
	$db->insert(	Piwik::prefixTable("access"),
			array(	"idsite" => $idSite, 
					"login" => $userLogin,
					"access" => 'admin')
	           );
	}
	self::insertSiteUrls($idSite, $urls);
	
	// we reload the access list which doesn't yet take in consideration this new website
	Zend_Registry::get('access')->loadAccess();

	return (int)$idSite;
}

(xsir317) #7

and the deleteSite

static public function deleteSite( $idSite )
{
//Piwik::checkUserIsSuperUser();
Piwik::checkUserHasAdminAccess($idSite);
$nbSites = count(Piwik_SitesManager_API::getSitesIdWithAdminAccess());

	if($nbSites == 1)
	{
		throw new Exception(Piwik_TranslateException("SitesManager_ExceptionDeleteSite"));
	}
	
	$db = Zend_Registry::get('db');
	
	$db->query("DELETE FROM ".Piwik::prefixTable("site")." 
				WHERE idsite = ?", $idSite);
	
	$db->query("DELETE FROM ".Piwik::prefixTable("site_url")." 
				WHERE idsite = ?", $idSite);
	
	$db->query("DELETE FROM ".Piwik::prefixTable("access")." 
				WHERE idsite = ?", $idSite);
	// TODO we should also delete all the data relative to this website...
	// post an event here that will be catched by the core and plugins to clean the data
}

(xsir317) #8

in fact,i was trying to create seveal pages to let common user add their own sites.but i think make some little change on the function addSite and deleteSite is enough.

seems the site-owner check is not required .even if a common user(like you,me ,or other guys) added a site like google or amazon,there is no problem.he cannot add the javascript code to the pages on google or amazon.
so there won’t be any data of this stupid site.


(kolchak) #9

Awesome - thanks, will take a look over the weekend (4 day weekend here in Aus, love it style_emoticons/<#EMO_DIR#>/smile.gif. I want sites to be able to auto register, so this will help! Will share what I end up with…


(xsir317) #10

[quote=kolchak @ Apr 9 2009, 05:59 AM) <{POST_SNAPBACK}>

Awesome - thanks, will take a look over the weekend (4 day weekend here in Aus, love it style_emoticons/<#EMO_DIR#>/smile.gif. I want sites to be able to auto register, so this will help! Will share what I end up with…

be able to auto register? i have done it.

in fact,you just get the userid, password,and get the email,

then like this :

QUOTE
//check if the user is exist $check_user_exist = $piwik_base_url."?module=API&method=UsersManager.userExists&userLogin=$login&format=PHP&prettyDisplay=true&token_auth=$sutoken_auth"; $result_check_user = file_get_contents($check_user_exist); //if not exist,create it if($result_check_user && preg_match('/false/',$result_check_user)) { $add_user =$piwik_base_url."?module=API&method=UsersManager.addUser&userLogin=$login&password=$commonpassword&email=$email&format=Html&token_auth=$sutoken_auth"; file_get_contents($add_user]?module=API&method=UsersManager.setUserAccess&userLogin=$login&access=admin&idSites=".$siteid[0]."&format=xml&token_auth=$super_user_token_auth[/quote]

hope it would help you…and my poor english didn’t puzzle you