Changeset 25195 for trunk


Ignore:
Timestamp:
Oct 28, 2013, 6:20:34 PM (10 years ago)
Author:
mistic100
Message:

feature 2976: new options for pwg.users.setInfo + user_id can be an array

Location:
trunk
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • trunk/include/ws_functions.inc.php

    r25118 r25195  
    33113311  }
    33123312 
    3313   if ($params['is_default'] !== null)
     3313  if (!empty($params['is_default']) or @$params['is_default']===false)
    33143314  {
    33153315    $updates['is_default'] = boolean_to_string($params['is_default']);
     
    35293529 
    35303530  // protect some users
    3531   $params['user_id'] = array_diff($params['user_id'], array($user['id'],
    3532                                                         $conf['guest_id'],
    3533                                                         $conf['default_user_id'],
    3534                                                         $conf['webmaster_id']));
     3531  $params['user_id'] = array_diff(
     3532    $params['user_id'],
     3533    array(
     3534      $user['id'],
     3535      $conf['guest_id'],
     3536      $conf['default_user_id'],
     3537      $conf['webmaster_id'],
     3538      )
     3539    );
    35353540 
    35363541  foreach ($params['user_id'] as $user_id)
     
    35483553 * API method
    35493554 * @param mixed[] $params
    3550  *    @option int user_id
     3555 *    @option int[] user_id
    35513556 *    @option string username (optional)
    35523557 *    @option string password (optional)
    35533558 *    @option string email (optional)
     3559 *    @option string status (optional)
     3560 *    @option int level (optional)
     3561 *    @option string language (optional)
     3562 *    @option string theme (optional)
     3563 *    @option int nb_image_page (optional)
     3564 *    @option int recent_period (optional)
     3565 *    @option bool expand (optional)
     3566 *    @option bool show_nb_comments (optional)
     3567 *    @option bool show_nb_hits (optional)
     3568 *    @option bool enabled_high (optional)
    35543569 */
    35553570function ws_users_setInfo($params, &$service)
     
    35593574  include_once(PHPWG_ROOT_PATH.'admin/include/functions.php');
    35603575 
    3561   if (get_username($params['user_id']) === false)
    3562   {
    3563     return new PwgError(WS_ERR_INVALID_PARAM, 'This user does not exist.');
    3564   }
    3565  
    35663576  $updates = $updates_infos = array();
    3567   $params = array_map('trim', $params);
    3568  
    3569   if (!empty($params['username']))
    3570   {
    3571     $user_id = get_userid($params['username']);
    3572     if ($user_id and $user_id != $params['user_id'])
    3573     {
    3574       return new PwgError(WS_ERR_INVALID_PARAM, l10n('this login is already used'));
    3575     }
    3576     if ($params['username'] != strip_tags($params['username']))
    3577     {
    3578       return new PwgError(WS_ERR_INVALID_PARAM, l10n('html tags are not allowed in login'));
    3579     }
    3580     $updates[ $conf['user_fields']['username'] ] = $params['username'];
    3581   }
    3582  
    3583   if (!empty($params['email']))
    3584   {
    3585     if ( ($error = validate_mail_address($params['user_id'], $params['email'])) != '')
    3586     {
    3587       return new PwgError(WS_ERR_INVALID_PARAM, $error);
    3588     }
    3589     $updates[ $conf['user_fields']['email'] ] = $params['email'];
    3590   }
    3591  
    3592   if (!empty($params['password']))
    3593   {
    3594     $updates[ $conf['user_fields']['password'] ] = $conf['password_hash']($params['password']);
     3577  $update_status = null;
     3578 
     3579  if (count($params['user_id']) == 1)
     3580  {
     3581    if (get_username($params['user_id'][0]) === false)
     3582    {
     3583      return new PwgError(WS_ERR_INVALID_PARAM, 'This user does not exist.');
     3584    }
     3585   
     3586    if (!empty($params['username']))
     3587    {
     3588      $user_id = get_userid($params['username']);
     3589      if ($user_id and $user_id != $params['user_id'][0])
     3590      {
     3591        return new PwgError(WS_ERR_INVALID_PARAM, l10n('this login is already used'));
     3592      }
     3593      if ($params['username'] != strip_tags($params['username']))
     3594      {
     3595        return new PwgError(WS_ERR_INVALID_PARAM, l10n('html tags are not allowed in login'));
     3596      }
     3597      $updates[ $conf['user_fields']['username'] ] = $params['username'];
     3598    }
     3599   
     3600    if (!empty($params['email']))
     3601    {
     3602      if ( ($error = validate_mail_address($params['user_id'][0], $params['email'])) != '')
     3603      {
     3604        return new PwgError(WS_ERR_INVALID_PARAM, $error);
     3605      }
     3606      $updates[ $conf['user_fields']['email'] ] = $params['email'];
     3607    }
     3608   
     3609    if (!empty($params['password']))
     3610    {
     3611      $updates[ $conf['user_fields']['password'] ] = $conf['password_hash']($params['password']);
     3612    }
    35953613  }
    35963614 
     
    36013619      return new PwgError(403, 'Only webmasters can grant "webmaster" status');
    36023620    }
    3603     if ( $user['id'] == $params['user_id'] )
    3604     {
    3605       $params['status'] = $user['status'];
    3606     }
    3607     if ( $conf['guest_id'] == $params['user_id'] )
    3608     {
    3609       $params['status'] = 'guest';
    3610     }
    3611     $updates_infos['status'] = $params['status'];
    3612   }
    3613  
    3614   if ($params['level'] !== null)
     3621    if ( !in_array($params['status'], array('generic','normal','admin','webmaster')) )
     3622    {
     3623      return new PwgError(WS_ERR_INVALID_PARAM, 'Invalid status');
     3624    }
     3625   
     3626    /*
     3627     * status update query is separated from the rest as not applying to the same
     3628     * set of users (current, guest and webmaster can't be changed)
     3629     */
     3630    $params['user_id_for_status'] = array_diff(
     3631      $params['user_id'],
     3632      array(
     3633        $user['id'],
     3634        $conf['guest_id'],
     3635        $conf['webmaster_id'],
     3636        )
     3637      );
     3638   
     3639    $update_status = $params['status'];
     3640  }
     3641 
     3642  if (!empty($params['level']) or @$params['level']===0)
    36153643  {
    36163644    if ( !in_array($params['level'], $conf['available_permission_levels']) )
     
    36213649  }
    36223650 
     3651  if (!empty($params['language']))
     3652  {
     3653    if ( !in_array($params['language'], array_keys(get_languages())) )
     3654    {
     3655      return new PwgError(WS_ERR_INVALID_PARAM, 'Invalid language');
     3656    }
     3657    $updates_infos['language'] = $params['language'];
     3658  }
     3659 
     3660  if (!empty($params['theme']))
     3661  {
     3662    if ( !in_array($params['theme'], array_keys(get_pwg_themes())) )
     3663    {
     3664      return new PwgError(WS_ERR_INVALID_PARAM, 'Invalid theme');
     3665    }
     3666    $updates_infos['theme'] = $params['theme'];
     3667  }
     3668 
     3669  if (!empty($params['nb_image_page']))
     3670  {
     3671    $updates_infos['nb_image_page'] = $params['nb_image_page'];
     3672  }
     3673 
     3674  if (!empty($params['recent_period']) or @$params['recent_period']===0)
     3675  {
     3676    $updates_infos['recent_period'] = $params['recent_period'];
     3677  }
     3678 
     3679  if (!empty($params['expand']) or @$params['expand']===false)
     3680  {
     3681    $updates_infos['expand'] = boolean_to_string($params['expand']);
     3682  }
     3683 
     3684  if (!empty($params['show_nb_comments']) or @$params['show_nb_comments']===false)
     3685  {
     3686    $updates_infos['show_nb_comments'] = boolean_to_string($params['show_nb_comments']);
     3687  }
     3688 
     3689  if (!empty($params['show_nb_hits']) or @$params['show_nb_hits']===false)
     3690  {
     3691    $updates_infos['show_nb_hits'] = boolean_to_string($params['show_nb_hits']);
     3692  }
     3693 
     3694  if (!empty($params['enabled_high']) or @$params['enabled_high']===false)
     3695  {
     3696    $updates_infos['enabled_high'] = boolean_to_string($params['enabled_high']);
     3697  }
     3698 
     3699  // perform updates
    36233700  single_update(
    36243701    USERS_TABLE,
    36253702    $updates,
    3626     array($conf['user_fields']['id'] => $params['user_id'])
     3703    array($conf['user_fields']['id'] => $params['user_id'][0])
    36273704    );
    36283705   
    3629   single_update(
    3630     USER_INFOS_TABLE,
    3631     $updates_infos,
    3632     array('user_id' => $params['user_id'])
    3633     );
     3706  if (isset($update_status) and count($params['user_id_for_status']) > 0)
     3707  {
     3708    $query = '
     3709UPDATE '. USER_INFOS_TABLE .' SET
     3710    status = "'. $update_status .'"
     3711  WHERE user_id IN('. implode(',', $params['user_id_for_status']) .')
     3712;';
     3713    pwg_query($query);
     3714  }
     3715 
     3716  if (count($updates_infos) > 0)
     3717  {
     3718    $query = '
     3719UPDATE '. USER_INFOS_TABLE .' SET ';
     3720
     3721    $first = true;
     3722    foreach ($updates_infos as $field => $value)
     3723    {
     3724      if (!$first) $query.= ', ';
     3725      else $first = false;
     3726      $query.= $field .' = "'. $value .'"';
     3727    }
     3728   
     3729    $query.= '
     3730  WHERE user_id IN('. implode(',', $params['user_id']) .')
     3731;';
     3732    pwg_query($query);
     3733  }
    36343734
    36353735  return $service->invoke('pwg.users.getList', array('user_id' => $params['user_id']));
  • trunk/ws.php

    r25118 r25195  
    847847      'ws_users_setInfo',
    848848      array(
    849         'user_id' =>  array('type'=>WS_TYPE_ID),
    850         'username' => array('flags'=>WS_PARAM_OPTIONAL),
    851         'password' => array('flags'=>WS_PARAM_OPTIONAL),
    852         'email' =>    array('flags'=>WS_PARAM_OPTIONAL),
    853         'status' =>   array('flags'=>WS_PARAM_OPTIONAL,
    854                             'info'=>'generic,normal,admin'),
    855         'level'=>     array('flags'=>WS_PARAM_OPTIONAL,
    856                             'maxValue'=>max($conf['available_permission_levels']),
    857                             'type'=>WS_TYPE_INT|WS_TYPE_POSITIVE),
    858         ),
    859       '<b>Admin & POST only.</b> Updates a user. Leave a field blank to keep the current value.',
     849        'user_id' =>          array('flags'=>WS_PARAM_FORCE_ARRAY,
     850                                    'type'=>WS_TYPE_ID),
     851        'username' =>         array('flags'=>WS_PARAM_OPTIONAL),
     852        'password' =>         array('flags'=>WS_PARAM_OPTIONAL),
     853        'email' =>            array('flags'=>WS_PARAM_OPTIONAL),
     854        'status' =>           array('flags'=>WS_PARAM_OPTIONAL,
     855                                    'info'=>'generic,normal,admin'),
     856        'level'=>             array('flags'=>WS_PARAM_OPTIONAL,
     857                                    'maxValue'=>max($conf['available_permission_levels']),
     858                                    'type'=>WS_TYPE_INT|WS_TYPE_POSITIVE),
     859        'language' =>         array('flags'=>WS_PARAM_OPTIONAL),
     860        'theme' =>            array('flags'=>WS_PARAM_OPTIONAL),
     861        // bellow are parameters removed in a future version
     862        'nb_image_page' =>    array('flags'=>WS_PARAM_OPTIONAL,
     863                                    'type'=>WS_TYPE_INT|WS_TYPE_POSITIVE|WS_TYPE_NOTNULL),
     864        'recent_period' =>    array('flags'=>WS_PARAM_OPTIONAL,
     865                                    'type'=>WS_TYPE_INT|WS_TYPE_POSITIVE),
     866        'expand' =>           array('flags'=>WS_PARAM_OPTIONAL,
     867                                    'type'=>WS_TYPE_BOOL),
     868        'show_nb_comments' => array('flags'=>WS_PARAM_OPTIONAL,
     869                                    'type'=>WS_TYPE_BOOL),
     870        'show_nb_hits' =>     array('flags'=>WS_PARAM_OPTIONAL,
     871                                    'type'=>WS_TYPE_BOOL),
     872        'enabled_high' =>     array('flags'=>WS_PARAM_OPTIONAL,
     873                                    'type'=>WS_TYPE_BOOL),
     874        ),
     875      '<b>Admin & POST only.</b> Updates a user. Leave a field blank to keep the current value.
     876<br>"username", "password" and "email" are ignored if "user_id" is an array.',
    860877      null,
    861878      array('admin_only'=>true, 'post_only'=>true)
Note: See TracChangeset for help on using the changeset viewer.